Java——DOM方式生成XML (轉)


http://blog.csdn.net/u012325167/article/details/50943202

使用DOM方式生成XML文件有如下幾步:

首先是創建DOM樹(即規定XML文件中的內容):

  • 創建DocumentBuilderFactory對象
  • 通過DocumentBuilderFactory對象創建DocumentBuilder對象
  • 通過DocumentBuilder對象的newDocument()方法創建一個Document對象,該對象代表一個XML文件
  • 通過Document對象的createElement()方法創建根節點
  • 通過Document對象的createElement()方法創建N個子節點,並為他們賦值,再將這些子節點添加到根節點下
  • 將根節點添加到Document對象下

其次是將DOM樹轉換為XML文件:

  • 創建TransformerFactory類的對象
  • 通過TransformerFactory創建Transformer對象
  • 使用Transformer對象的transform()方法將DOM樹轉換為XML文件。(該方法有兩個參數,第一個參數為源數據,需要創建DOMSource對象並將Document加載到其中;第二個參數為目的文件,即要生成的XML文件,需要創建StreamResult對象並指定目的文件)

下面開始實現:

首先是創建DOM樹的部分:

創建DocumentBuilderFactory類的對象:

// 創建DocumentBuilderFactory DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
  • 1
  • 2
  • 1
  • 2

創建DocumentBuilder對象:

// 創建DocumentBuilder DocumentBuilder builder = factory.newDocumentBuilder();
  • 1
  • 2
  • 1
  • 2

創建Document:

// 創建Document Document document = builder.newDocument();
  • 1
  • 2
  • 1
  • 2

創建根節點:

// 創建根節點 Element bookstore = document.createElement("bookstore");
  • 1
  • 2
  • 1
  • 2

創建子節點並添加屬性:
通過Document的createElement()方法可創建節點,通過Element的setAttribute()方法可設置屬性。

// 創建子節點,並設置屬性
Element book = document.createElement("book"); book.setAttribute("id", "1");
  • 1
  • 2
  • 3
  • 1
  • 2
  • 3

為book節點添加子節點:
通過Element的setTextContent()方法可設置節點之間的內容(節點的值),通過Element的appendChild()方法可為節點添加子節點。

// 為book添加子節點
Element name = document.createElement("name"); name.setTextContent("安徒生童話"); book.appendChild(name); Element author = document.createElement("author"); author.setTextContent("安徒生"); book.appendChild(author); Element price = document.createElement("price"); price.setTextContent("49"); book.appendChild(price);
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

為根節點添加子節點:

// 為根節點添加子節點 bookstore.appendChild(book);
  • 1
  • 2
  • 1
  • 2

將根節點添加到Document下:

// 將根節點添加到Document下 document.appendChild(bookstore);
  • 1
  • 2
  • 1
  • 2

至此,DOM樹就生成完了。


下面開始生成XML文件:

創建TransformerFactory對象:

// 創建TransformerFactory對象 TransformerFactory tff = TransformerFactory.newInstance();
  • 1
  • 2
  • 1
  • 2

創建Transformer對象:

// 創建Transformer對象 Transformer tf = tff.newTransformer();
  • 1
  • 2
  • 1
  • 2

使用Transformer對象的transform()方法將DOM樹轉換成XML:

// 使用Transformer的transform()方法將DOM樹轉換成XML tf.transform(new DOMSource(document), new StreamResult(dest));
  • 1
  • 2
  • 1
  • 2

至此,就完成了對XML文件的生成。


運行結果如下:

<?xml version="1.0" encoding="UTF-8" standalone="no"?><bookstore><book id="1"><name>安徒生童話</name><author>安徒生</author><price>49</price></book></bookstore>
  • 1
  • 1

可見XML文件生成成功了。但是,文件中的內容是一整行的,沒有換行,不易讀。
這時,我們需要使用Transformer的setOutputProperty()方法設置輸出屬性,設置為換行即可。

// 設置輸出數據時換行
tf.setOutputProperty(OutputKeys.INDENT, "yes");
  • 1
  • 2
  • 1
  • 2

再次運行,可見XML文件中內容已正確換行:

<?xml version="1.0" encoding="UTF-8" standalone="no"?> <bookstore> <book id="1"> <name>安徒生童話</name> <author>安徒生</author> <price>49</price> </book> </bookstore>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

此外,可以看到,在XML文件的聲明部分中,有standalone屬性,該屬性表示當前XML是否有相對應的dtd或schema文件作為其說明文檔。

若屬性值為“yes”,則代表當前XML文件沒有dtd和schema文件作為其說明文檔。

由於我們這里沒有用到dtd和schema,所以我們可以通過Document的setXmlStandalone()方法將該屬性隱藏掉。

// 設置XML聲明中standalone為yes,即沒有dtd和schema作為該XML的說明文檔,且不顯示該屬性 document.setXmlStandalone(true);
  • 1
  • 2
  • 1
  • 2

再次測試,可發現該已經沒有該屬性了。


下面給出完整的代碼:

package util; import java.io.File; 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; public class FileUtil { public void createXMLByDOM(File dest) { // 創建DocumentBuilderFactory DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); try { // 創建DocumentBuilder DocumentBuilder builder = factory.newDocumentBuilder(); // 創建Document Document document = builder.newDocument(); // 設置XML聲明中standalone為yes,即沒有dtd和schema作為該XML的說明文檔,且不顯示該屬性 // document.setXmlStandalone(true); // 創建根節點 Element bookstore = document.createElement("bookstore"); // 創建子節點,並設置屬性 Element book = document.createElement("book"); book.setAttribute("id", "1"); // 為book添加子節點 Element name = document.createElement("name"); name.setTextContent("安徒生童話"); book.appendChild(name); Element author = document.createElement("author"); author.setTextContent("安徒生"); book.appendChild(author); Element price = document.createElement("price"); price.setTextContent("49"); book.appendChild(price); // 為根節點添加子節點 bookstore.appendChild(book); // 將根節點添加到Document下 document.appendChild(bookstore); /* * 下面開始實現: 生成XML文件 */ // 創建TransformerFactory對象 TransformerFactory tff = TransformerFactory.newInstance(); // 創建Transformer對象 Transformer tf = tff.newTransformer(); // 設置輸出數據時換行 tf.setOutputProperty(OutputKeys.INDENT, "yes"); // 使用Transformer的transform()方法將DOM樹轉換成XML tf.transform(new DOMSource(document), new StreamResult(dest)); } catch (ParserConfigurationException e) { e.printStackTrace(); } catch (TransformerConfigurationException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (TransformerException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM