例子-
<Envision>
<Employee>
<Employee-code>Shiva kumar</Employee-code>
<Employee-Name>474</Employee-Name>
</Employee>
<Employee>
<Employee-code>Santhosh Kumar</Employee-code>
<Employee-Name>475</Employee-Name>
</Employee>
</Envision>
我想创建一个 xml 文件,如上所示 xml。但我只能这样做 -
<Employee>
<Employee-code>Shiva kumar</Employee-code>
<Employee-name>474</Employee-name>
<Employee>
</Envision>
通过以下代码-
DocumentBuilderFactory 工厂=DocumentBuilderFactory.newInstance();
DocumentBuilder builder=factory.newDocumentBuilder();
Document doc=builder.newDocument();
Element root=doc.createElement("Envision");
doc.appendChild(root);
Element ele=doc.createElement("Employee_Name");
ele.appendChild(doc.createTextNode("Shiva Kumar"));
root.appendChild(ele);
ele=doc.createElement("Employee_Code");
ele.appendChild(doc.createTextNode("474"));
root.appendChild(ele);
TransformerFactory transformerFactory =TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(System.out);
transformer.transform(source, result);
每当我想通过将下面的代码附加到员工代码标记之后的上面代码来向该代码添加第二个块时
Element root1=doc.createElement("Envision");
doc.appendChild(root1);
ele=doc.createElement("Employee_Name");
ele.appendChild(doc.createTextNode("Vijay Babu"));
root1.appendChild(ele);
ele=doc.createElement("Employee_Code");
ele.appendChild(doc.createTextNode(""));
root1.appendChild(ele);
然后我收到一条错误消息,例如“不允许创建节点”。在这种情况下请帮助我