如何用Java(DOM Parser)编写XML文件
时间:2020-02-23 14:36:15 来源:igfitidea点击:
之前我们学习了如何使用DOM分析器读取XML文件以及如何在Java中编辑XML文件,今天我们将学习如何使用DOM分析器用Java语言编写XML文件。
这是我们XML文件的要求。
根元素将是名称空间为" https://www.theitroad.local/employee"的"雇员"。
该根元素将包含员工列表。员工信息将写在"员工"元素中。
XML文件中将有两个员工信息。每个员工都有一个名为" id"的属性
雇员元素将具有四个元素-"姓名","年龄","角色","性别"。
这是上述要求的Java程序。
package com.theitroad.xml; import java.io.File; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.transform.OutputKeys; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerFactory; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; public class XMLWriterDOM { public static void main(String[] args) { DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder dBuilder; try { dBuilder = dbFactory.newDocumentBuilder(); Document doc = dBuilder.newDocument(); //add elements to Document Element rootElement = doc.createElementNS("https://www.theitroad.local/employee", "Employees"); //append root element to document doc.appendChild(rootElement); //append first child element to root element rootElement.appendChild(getEmployee(doc, "1", "hyman", "29", "Java Developer", "Male")); //append second child rootElement.appendChild(getEmployee(doc, "2", "Lisa", "35", "Manager", "Female")); //for output to file, console TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer(); //for pretty print transformer.setOutputProperty(OutputKeys.INDENT, "yes"); DOMSource source = new DOMSource(doc); //write to console or file StreamResult console = new StreamResult(System.out); StreamResult file = new StreamResult(new File("/Users/hyman/emps.xml")); //write data transformer.transform(source, console); transformer.transform(source, file); System.out.println("DONE"); } catch (Exception e) { e.printStackTrace(); } } private static Node getEmployee(Document doc, String id, String name, String age, String role, String gender) { Element employee = doc.createElement("Employee"); //set id attribute employee.setAttribute("id", id); //create name element employee.appendChild(getEmployeeElements(doc, employee, "name", name)); //create age element employee.appendChild(getEmployeeElements(doc, employee, "age", age)); //create role element employee.appendChild(getEmployeeElements(doc, employee, "role", role)); //create gender element employee.appendChild(getEmployeeElements(doc, employee, "gender", gender)); return employee; } //utility method to create text node private static Node getEmployeeElements(Document doc, Element element, String name, String value) { Element node = doc.createElement(name); node.appendChild(doc.createTextNode(value)); return node; } }
请注意,我创建了两个StreamResult,一个用于在控制台中打印XML以进行调试,另一个用于将XML写入文件。
这是上述程序的输出XML。
<?xml version="1.0" encoding="UTF-8" standalone="no"?> <Employees xmlns="https://www.theitroad.local/employee"> <Employee id="1"> <name>hyman</name> <age>29</age> <role>Java Developer</role> <gender>Male</gender> </Employee> <Employee id="2"> <name>Lisa</name> <age>35</age> <role>Manager</role> <gender>Female</gender> </Employee> </Employees>