Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用DocumentBuilderFactory在java中创建具有多节点结构的xml文件_Java_Xml - Fatal编程技术网

如何使用DocumentBuilderFactory在java中创建具有多节点结构的xml文件

如何使用DocumentBuilderFactory在java中创建具有多节点结构的xml文件,java,xml,Java,Xml,我的要求是使用DocumentBuilderFactory创建类似这种结构的xml文件 <?xml version="1.0" encoding="UTF-8"?> <Employees> <EmpInfo_Dept1> <Employee name="Luisha" id="D111" salary="20000"/> <Employee name="Lisha" id="D112" salary="50000

我的要求是使用
DocumentBuilderFactory
创建类似这种结构的xml文件

<?xml version="1.0" encoding="UTF-8"?>

<Employees>
   <EmpInfo_Dept1>
     <Employee name="Luisha" id="D111" salary="20000"/>
      <Employee name="Lisha" id="D112" salary="50000"/>
   </EmpInfo_Dept1>

  <EmpInfo_Dept2>
     <Employee name="Jack" id="D211" salary="20000"/>
      <Employee name="Johnson" id="D212" salary="50000"/>
   </EmpInfo_Dept2>
</Employees>

尝试以下代码片段:

public class Example {
    public static void main(String[] args) throws ParserConfigurationException, TransformerException {
        DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
        Document doc = docBuilder.newDocument();

        Element rootElement = doc.createElement("Employees");
        doc.appendChild(rootElement);

        Element empInfoDept1 = doc.createElement("EmpInfo_Dept1");
        rootElement.appendChild(empInfoDept1);
        Element employee1 = createEmployeeElement(doc, "Luisha", "D111", "20000");
        empInfoDept1.appendChild(employee1);
        Element employee2 = createEmployeeElement(doc, "Lisha", "D112", "50000");
        empInfoDept1.appendChild(employee2);

        Element empInfoDept2 = doc.createElement("EmpInfo_Dept2");
        rootElement.appendChild(empInfoDept2);
        Element employee3 = createEmployeeElement(doc, "Jack", "D211", "20000");
        empInfoDept2.appendChild(employee3);
        Element employee4 = createEmployeeElement(doc, "Johnson", "D212", "50000");
        empInfoDept2.appendChild(employee4);

        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        DOMSource source = new DOMSource(doc);
        StreamResult result = new StreamResult(new File("file.xml"));
        transformer.transform(source, result);
    }

    public static Element createEmployeeElement(Document doc, String name, String id, String salary) {
        Element staff = doc.createElement("Employee");

        Attr nameAttr = doc.createAttribute("name");
        nameAttr.setValue(name);
        staff.setAttributeNode(nameAttr);

        Attr idAttr = doc.createAttribute("id");
        idAttr.setValue(id);
        staff.setAttributeNode(idAttr);

        Attr salaryAttr = doc.createAttribute("salary");
        salaryAttr.setValue(salary);
        staff.setAttributeNode(salaryAttr);
        return staff;
    }
}