简体   繁体   English

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

[英]how to create a xml file with multi node structure in java using DocumentBuilderFactory

My requirement is to create a xml file like this structure by using DocumentBuilderFactory . 我的要求是通过使用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>

try following snippet: 尝试以下代码段:

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;
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用DocumentBuilderFactory将节点附加到现有xml-Java - Append Node to Existing xml using DocumentBuilderFactory - Java 无法使用Java中的DocumentBuilderFactory解析XML的内部元素 - Not able to parse inner elements of XML using DocumentBuilderFactory in Java 如何动态地从java创建xml文件中的节点中的节点 - how to create node in a node in a xml file from java dynamically 使用DocumentBuilderFactory进行Java XML解析 - 空节点? - Java XML parsing with DocumentBuilderFactory - null nodes? 如何使用Java创建xml文件? - How to create xml file using Java? 如何在Java或任何其他工具中将具有不同节点结构的多个xml文件合并为单个xml文件? - How to merge multiple xml files with different node structure into a single xml file in java or any other tool? 使用DocumentBuilderFactory将XML文档转换为DOM对象 - Xml document to DOM object using DocumentBuilderFactory 如何使用Java创建一个非常具体的zip文件结构 - How to create a very specific zip file structure using Java 当我尝试使用 DocumentBuilderFactory 按模式验证 xml 文件时,我收到错误“找不到元素的声明” - When i try to validate xml file using DocumentBuilderFactory by schema i recieve error "Cannot find the declaration of element " 如何停止Java小程序发出对诸如javax.xml.parsers.DocumentBuilderFactory之类的服务的请求 - How to stop Java applet making request for services like javax.xml.parsers.DocumentBuilderFactory
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM