如何使用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内容:
Pankaj 29 Java Developer Male Lisa 35 Manager Female
该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内容:
Pankaj 29 Java Developer Male Lisa 35 Manager Female
关于如何使用DOM方式生成XML文件问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注创新互联行业资讯频道了解更多相关知识。
文章题目:如何使用DOM方式生成XML文件
文章链接:http://myzitong.com/article/goopoj.html