Java 操纵XML之创建XML文件

时间:2022-03-27 20:56:23

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

 package cn.com.zfc.lesson26.xml;

 import java.io.File;

 import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
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.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Text; /**
* 使用JAVA DOM PARSER:创建 XML 文件
*
* @author zfc
* @date 2017年12月7日 下午6:11:27
*/
public class CreateXmlFile {
public static void main(String[] args) { try {
// 1、创建 DocumentBuilderFactory 对象
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
// 2、创建 DocumentBuilder 对象
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
// 3、创建 Document 对象
Document document = documentBuilder.newDocument();
// 4、创建元素,创建一个根元素
Element students = document.createElement("students");
// 5、将根元素添加到文档对象中
document.appendChild(students); // 6、创建第一个学生
Element student = document.createElement("student");
// 创建一个属性对象
Attr attr = document.createAttribute("id");
// 给属性设值
attr.setValue("student1");
// 将属性添加到 student 结点上
student.setAttributeNode(attr);
// 创建 name 子结点
Element name = document.createElement("name");
// 创建文本结点
Text nameValue = document.createTextNode("Tom");
// 将文本结点添加到 name 结点上
name.appendChild(nameValue);
// 将 name 结点添加到 student 结点上
student.appendChild(name);
// 创建 sex 结点
Element sex = document.createElement("sex");
// 创建 文本结点
Text sexValue = document.createTextNode("Female");
// 将 sexValue 添加到 sex 结点上
sex.appendChild(sexValue);
// 将 sex 结点添加到 student 结点上
student.appendChild(sex);
// 将 student 结点添加到 students 结点中
students.appendChild(student); // 7、添加第二个学生
student = document.createElement("student");
// 创建一个属性对象
attr = document.createAttribute("id");
// 给属性设值
attr.setValue("student2");
// 将属性添加到 student 结点上
student.setAttributeNode(attr);
// 创建 name 子结点
name = document.createElement("name");
// 创建文本结点
nameValue = document.createTextNode("Lucy");
// 将文本结点添加到 name 结点上
name.appendChild(nameValue);
// 将 name 结点添加到 student 结点上
student.appendChild(name);
// 创建 sex 结点
sex = document.createElement("sex");
// 创建 文本结点
sexValue = document.createTextNode("Male");
// 将 sexValue 添加到 sex 结点上
sex.appendChild(sexValue);
// 将 sex 结点添加到 student 结点上
student.appendChild(sex);
// 将 student 结点添加到 students 结点中
students.appendChild(student); // 8、创建 TransformerFactory 对象
TransformerFactory transformerFactory = TransformerFactory.newInstance();
// 9、创建 Transformer 对象
Transformer transformer = transformerFactory.newTransformer();
// 10、创建 DOMSource 对象
DOMSource domSource = new DOMSource(document);
// 11、创建 File 对象
String filePath = "I:\\code_workspace\\JavaSE_workspace\\JavaSE\\src\\cn\\com\\zfc\\lesson26\\xml\\CreateXmlFile.xml";
File file = new File(filePath);
// 12、创建 StreamResult 对象
StreamResult reStreamResult = new StreamResult(file);
transformer.transform(domSource, reStreamResult);
// 输出测试结果
StreamResult consoleResult = new StreamResult(System.out);
transformer.transform(domSource, consoleResult); } catch (Exception e) {
e.printStackTrace();
} }
}

三、运行结果:CreateXmlFile.xml

 <?xml version="1.0" encoding="UTF-8" standalone="no"?>
<students>
<student id="student1">
<name>Tom</name>
<sex>Female</sex>
</student>
<student id="student2">
<name>Lucy</name>
<sex>Male</sex>
</student>
</students>

Java 操纵XML之创建XML文件