您好,登錄后才能下訂單哦!
如何使用DOM方式生成XML文件?針對這個問題,這篇文章詳細介紹了相對應的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。
下面是我們對要生成的XML文件的具體要求:
1.根節點元素為”Employees”,命名空間為”http://www.php.cn/“,根節點下包含一系列的Employee元素。
2.員工的信息通過Employee節點表示,生成的XML文件有兩條員工的信息。
3.每個員工有個”id”屬性。
4.Employee 元素有四個子元素- “name”, “age”, “role”, “gender”。
下面是程序代碼:
package com.journaldev.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("http://www.journaldev.com/employee", "Employees"); //append root element to document doc.appendChild(rootElement); //append first child element to root element rootElement.appendChild(getEmployee(doc, "1", "Pankaj", "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/pankaj/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=" <Employee id="1"><name>Pankaj</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>
該XML內容沒有格式化,如果你需要對XML內容做適當的格式化,參考這篇文章Java中格式化XML
在前面的教程中,我們學習了使用DOM解析方式讀取和修改XML文件內容,今天我們來學習如何使用DOM解析機制生成XML文件。
下面是我們對要生成的XML文件的具體要求:
1.根節點元素為”Employees”,命名空間為”http://www.php.cn/“,根節點下包含一系列的Employee元素。
2.員工的信息通過Employee節點表示,生成的XML文件有兩條員工的信息。
3.每個員工有個”id”屬性。
4.Employee 元素有四個子元素- “name”, “age”, “role”, “gender”。
下面是程序代碼:
package com.journaldev.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("http://www.journaldev.com/employee", "Employees"); //append root element to document doc.appendChild(rootElement); //append first child element to root element rootElement.appendChild(getEmployee(doc, "1", "Pankaj", "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/pankaj/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=" <Employee id="1"><name>Pankaj</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>
關于如何使用DOM方式生成XML文件問題的解答就分享到這里了,希望以上內容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關注億速云行業資訊頻道了解更多相關知識。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。