如何使用DOM方式生成XML文件?針對這個問題,這篇文章詳細介紹了相對應的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。
成都網(wǎng)站建設、成都網(wǎng)站制作的關注點不是能為您做些什么網(wǎng)站,而是怎么做網(wǎng)站,有沒有做好網(wǎng)站,給成都創(chuàng)新互聯(lián)一個展示的機會來證明自己,這并不會花費您太多時間,或許會給您帶來新的靈感和驚喜。面向用戶友好,注重用戶體驗,一切以用戶為中心。
下面是我們對要生成的XML文件的具體要求:
1.根節(jié)點元素為”Employees”,命名空間為”http://www.php.cn/“,根節(jié)點下包含一系列的Employee元素。
2.員工的信息通過Employee節(jié)點表示,生成的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; } }
需要注意的是代碼中創(chuàng)建了兩個StreamResult對象,一個是出于調試的目的將XML文件內容輸出到控制臺中,另一個將XML內容寫到文件中。
程序輸出的XML內容:
Pankaj 29 Java Developer Male Lisa 35 Manager Female
該XML內容沒有格式化,如果你需要對XML內容做適當?shù)母袷交?,參考這篇文章Java中格式化XML
在前面的教程中,我們學習了使用DOM解析方式讀取和修改XML文件內容,今天我們來學習如何使用DOM解析機制生成XML文件。
下面是我們對要生成的XML文件的具體要求:
1.根節(jié)點元素為”Employees”,命名空間為”http://www.php.cn/“,根節(jié)點下包含一系列的Employee元素。
2.員工的信息通過Employee節(jié)點表示,生成的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; } }
需要注意的是代碼中創(chuàng)建了兩個StreamResult對象,一個是出于調試的目的將XML文件內容輸出到控制臺中,另一個將XML內容寫到文件中。
程序輸出的XML內容:
Pankaj 29 Java Developer Male Lisa 35 Manager Female
關于如何使用DOM方式生成XML文件問題的解答就分享到這里了,希望以上內容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關注創(chuàng)新互聯(lián)行業(yè)資訊頻道了解更多相關知識。