Java——DOM方式產生XML

來源:互聯網
上載者:User

標籤:package   roc   sso   utf-8   ntb   nts   value   result   div   

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類的對象:

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

建立DocumentBuilder對象:

// 建立DocumentBuilderDocumentBuilder builder = factory.newDocumentBuilder();
  • 1
  • 2
  • 1
  • 2

建立Document:

// 建立DocumentDocument 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樹轉換成XMLtf.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

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.