package com.gyj.jdom; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import javax.xml.transform.OutputKeys; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerConfigurationException; import javax.xml.transform.TransformerException; 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; /** * 写入DOM到XML文件 * @author GYJ * @date 2014-3-23 */ public class XMLWriterDOM { /** * 将会写入这么一份文件 * <p>1、根元素为"Employees" , 命名空间为"http://www.miamiheat.com" * 2、创建两份员工,都有id属性 * 3、内容都有姓名、年龄、角色 * 4、写出数据到文件、打印到控制台 * @param args * @throws TransformerException */ public static void main(String[] args) throws TransformerException { DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder dBuilder; try { dBuilder = dbFactory.newDocumentBuilder(); Document doc = dBuilder.newDocument(); //添加根元素 Element rootElement = doc.createElementNS("http://www.miamiheat.com", "Employees"); doc.appendChild(rootElement); //添加子元素到根目录下 rootElement.appendChild(getEmployee(doc, "1", "gyj", "18")); rootElement.appendChild(getEmployee(doc, "2", "james", "29")); //把创建好的文件输出 TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); DOMSource domSource = new DOMSource(doc); //控制台输出 StreamResult console = new StreamResult(System.out); //输出到文件 StreamResult file = new StreamResult("emp.xml"); //写数据 transformer.transform(domSource, console); transformer.transform(domSource, file); } catch (ParserConfigurationException e) { e.printStackTrace(); } //out put============================================== /*<?xml version="1.0" encoding="UTF-8" standalone="no"?> <Employees xmlns="http://www.miamiheat.com"> <employee id="1"> <name>gyj</name> <age>18</age> </employee> <employee id="2"> <name>james</name> <age>29</age> </employee> </Employees>*/ //====================================================== //生成了emp.xml /*<?xml version="1.0" encoding="UTF-8" standalone="no"?> <Employees xmlns="http://www.miamiheat.com"> <employee id="1"> <name>gyj</name> <age>18</age> </employee> <employee id="2"> <name>james</name> <age>29</age> </employee> </Employees>*/ } private static Node getEmployee(Document doc, String id, String name, String age) { //创建子节点 Element employee = doc.createElement("employee"); employee.setAttribute("id", id); //创建字节点的元素 employee.appendChild(getEmployeeElements(doc, employee, "name", name)); employee.appendChild(getEmployeeElements(doc, employee, "age", age)); return employee; } /** * 创建子元素节点 * @param doc 文档对象 * @param element 子元素的tag * @param name 子元素节点 * @param value 子元素节点值 * @return */ private static Node getEmployeeElements(Document doc, Element element, String name, String value) { Element node = doc.createElement(name); node.appendChild(doc.createTextNode(value)); return node; } }