Java 操纵XML之创建XML文件

Java 操纵XML之创建XML文件

一、JAVA DOM PARSER

DOM interfaces
  The DOM defines several Java interfaces. Here are the most common interfaces:
    Node - The base datatype of the DOM.
    Element - The vast majority of the objects you'll deal with are Elements.
    Attr Represents an attribute of an element.
    Text The actual content of an Element or Attr.
    Document Represents the entire XML document. A Document object is often referred to as a DOM tree.
Common DOM methods
  When you are working with the DOM, there are several methods you'll use often:
    Document.getDocumentElement() - Returns the root element of the document.
    Node.getFirstChild() - Returns the first child of a given Node.
    Node.getLastChild() - Returns the last child of a given Node.
    Node.getNextSibling() - These methods return the next sibling of a given Node.
    Node.getPreviousSibling() - These methods return the previous sibling of a given Node.
    Node.getAttribute(attrName) - For a given Node, returns the attribute with the requested name.

二、源代码:CreateXmlFile.java

  1 package cn.com.zfc.lesson26.xml;
  2 
  3 import java.io.File;
  4 
  5 import javax.xml.parsers.DocumentBuilder;
  6 import javax.xml.parsers.DocumentBuilderFactory;
  7 import javax.xml.transform.Transformer;
  8 import javax.xml.transform.TransformerFactory;
  9 import javax.xml.transform.dom.DOMSource;
 10 import javax.xml.transform.stream.StreamResult;
 11 
 12 import org.w3c.dom.Attr;
 13 import org.w3c.dom.Document;
 14 import org.w3c.dom.Element;
 15 import org.w3c.dom.Text;
 16 
 17 /**
 18  * 使用JAVA DOM PARSER:创建 XML 文件
 19  * 
 20  * @author zfc
 21  * @date 2017年12月7日 下午6:11:27
 22  */
 23 public class CreateXmlFile {
 24     public static void main(String[] args) {
 25 
 26         try {
 27             // 1、创建 DocumentBuilderFactory 对象
 28             DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
 29             // 2、创建 DocumentBuilder 对象
 30             DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
 31             // 3、创建 Document 对象
 32             Document document = documentBuilder.newDocument();
 33             // 4、创建元素,创建一个根元素
 34             Element students = document.createElement("students");
 35             // 5、将根元素添加到文档对象中
 36             document.appendChild(students);
 37 
 38             // 6、创建第一个学生
 39             Element student = document.createElement("student");
 40             // 创建一个属性对象
 41             Attr attr = document.createAttribute("id");
 42             // 给属性设值
 43             attr.setValue("student1");
 44             // 将属性添加到 student 结点上
 45             student.setAttributeNode(attr);
 46             // 创建 name 子结点
 47             Element name = document.createElement("name");
 48             // 创建文本结点
 49             Text nameValue = document.createTextNode("Tom");
 50             // 将文本结点添加到 name 结点上
 51             name.appendChild(nameValue);
 52             // 将 name 结点添加到 student 结点上
 53             student.appendChild(name);
 54             // 创建 sex 结点
 55             Element sex = document.createElement("sex");
 56             // 创建 文本结点
 57             Text sexValue = document.createTextNode("Female");
 58             // 将 sexValue 添加到 sex 结点上
 59             sex.appendChild(sexValue);
 60             // 将 sex 结点添加到 student 结点上
 61             student.appendChild(sex);
 62             // 将 student 结点添加到 students 结点中
 63             students.appendChild(student);
 64 
 65             // 7、添加第二个学生
 66             student = document.createElement("student");
 67             // 创建一个属性对象
 68             attr = document.createAttribute("id");
 69             // 给属性设值
 70             attr.setValue("student2");
 71             // 将属性添加到 student 结点上
 72             student.setAttributeNode(attr);
 73             // 创建 name 子结点
 74             name = document.createElement("name");
 75             // 创建文本结点
 76             nameValue = document.createTextNode("Lucy");
 77             // 将文本结点添加到 name 结点上
 78             name.appendChild(nameValue);
 79             // 将 name 结点添加到 student 结点上
 80             student.appendChild(name);
 81             // 创建 sex 结点
 82             sex = document.createElement("sex");
 83             // 创建 文本结点
 84             sexValue = document.createTextNode("Male");
 85             // 将 sexValue 添加到 sex 结点上
 86             sex.appendChild(sexValue);
 87             // 将 sex 结点添加到 student 结点上
 88             student.appendChild(sex);
 89             // 将 student 结点添加到 students 结点中
 90             students.appendChild(student);
 91 
 92             // 8、创建 TransformerFactory 对象
 93             TransformerFactory transformerFactory = TransformerFactory.newInstance();
 94             // 9、创建 Transformer 对象
 95             Transformer transformer = transformerFactory.newTransformer();
 96             // 10、创建 DOMSource 对象
 97             DOMSource domSource = new DOMSource(document);
 98             // 11、创建 File 对象
 99             String filePath = "I:\\code_workspace\\JavaSE_workspace\\JavaSE\\src\\cn\\com\\zfc\\lesson26\\xml\\CreateXmlFile.xml";
100             File file = new File(filePath);
101             // 12、创建 StreamResult 对象
102             StreamResult reStreamResult = new StreamResult(file);
103             transformer.transform(domSource, reStreamResult);
104             // 输出测试结果
105             StreamResult consoleResult = new StreamResult(System.out);
106             transformer.transform(domSource, consoleResult);
107 
108         } catch (Exception e) {
109             e.printStackTrace();
110         }
111 
112     }
113 }

三、运行结果:CreateXmlFile.xml

 1 <?xml version="1.0" encoding="UTF-8" standalone="no"?>
 2 <students>
 3     <student id="student1">
 4         <name>Tom</name>
 5         <sex>Female</sex>
 6     </student>
 7     <student id="student2">
 8         <name>Lucy</name>
 9         <sex>Male</sex>
10     </student>
11 </students>

posted @ 2017-12-07 21:27  勇闯天涯zfc  阅读(231)  评论(0编辑  收藏  举报