Java解析XML文檔——dom解析xml

來源:互聯網
上載者:User

一、前言  

  用Java解析XML文檔,最常用的有兩種方法:使用基於事件的XML簡單API(Simple API for XML)稱為SAX和基於樹和節點的文件物件模型(Document Object Module)稱為DOM。Sun公司提供了Java API for XML Parsing(JAXP)介面來使用SAX和DOM,通過JAXP,我們可以使用任何與JAXP相容的XML解析器。 

  JAXP介面包含了三個包:  

  (1)org.w3c.dom W3C推薦的用於XML標準規劃文件物件模型的介面。  

  (2)org.xml.sax  用於對XML進行文法分析的事件驅動的XML簡單API(SAX)  

  (3)javax.xml.parsers解析器工廠工具,程式員獲得並配置特殊的特殊文法分析器。  

  二、前提  

  DOM編程不要其它的依賴包,因為JDK裡內建的JDK裡含有的上面提到的org.w3c.dom、org.xml.sax 和javax.xml.parsers包就可以滿意條件了。  

  三、使用DOM解析XML文檔  

  我們現在來看看DOM是如何解析XML的吧!同樣的,我將從一個簡單的不能再簡單的例子來說明DOM是如何解析XML文檔的,先讓我們看看XML是什麼內容吧:

<?xml version="1.0" encoding="gb2312"?>  

<books>  

<book email="zhoujunhui">  

<name>rjzjh</name>  

<price>jjjjjj</price>  

</book>  

</books>

public void useDomParseXml() {

DocumentBuilderFactory domfac = DocumentBuilderFactory.newInstance();

try {

DocumentBuilder dombuilder=domfac.newDocumentBuilder();

InputStream is = new FileInputStream("bin/library.xml");

Document doc = dombuilder.parse(is);

Element root=doc.getDocumentElement();

NodeList books=root.getChildNodes();

if (books != null) {

for (int i = 0; i < books.getLength(); i++) {

Node book = books.item(i);

if (book.getNodeType() == Node.ELEMENT_NODE) {

String email = book.getAttributes().getNamedItem(

"email").getNodeValue();

System.out.println(email);

for (Node node = book.getFirstChild(); node != null;

node = node.getNextSibling()) {

if(node.getNodeType()==Node.ELEMENT_NODE){

if (node.getNodeName().equals("name")) {

String name = node.getFirstChild().

getNodeValue();

System.out.println(name);

}

if (node.getNodeName().equals("price")) {

String price = node.getFirstChild().

getNodeValue();

System.out.println(price);

}

}

}

}

}

}

} catch (Exception e) {

}

}

(1)得到DOM解析器的工廠執行個體  

  DocumentBuilderFactory domfac=DocumentBuilderFactory.newInstance();  

  得到javax.xml.parsers.DocumentBuilderFactory;類的執行個體就是我們要的解析器工廠  

  (2)從DOM工廠獲得DOM解析器  

  DocumentBuilder dombuilder=domfac.newDocumentBuilder();  

  通過javax.xml.parsers.DocumentBuilderFactory執行個體的靜態方法newDocumentBuilder()得到DOM解析器  

  (3)把要解析的XML文檔轉化為輸入資料流,以便DOM解析器解析它  

  InputStream is=new FileInputStream("bin/library.xml");  

  InputStream是一個介面。  

  (4)解析XML文檔的輸入資料流,得到一個Document  

  Document doc=dombuilder.parse(is); 

  由XML文檔的輸入資料流得到一個org.w3c.dom.Document對象,以後的處理都是對Document對象進行的 

  (5)得到XML文檔的根節點  

  Element root=doc.getDocumentElement();  

  在DOM中只有根節點是一個org.w3c.dom.Element對象。

  (6)得到節點的子節點  

  NodeList books=root.getChildNodes();  

  for(int i=0;i<books.getLength();i++){  

  Node book=books.item(i);  

  }

  這是用一個org.w3c.dom.NodeList介面來存放它所有子節點的,還有一種輪循子節點的方法,後面有介紹  

  (7)取得節點的屬性值  

  String email=book.getAttributes().getNamedItem("email").getNodeValue();  

  System.out.println(email);  

  注意,節點的屬性也是它的子節點。它的節點類型也是Node.ELEMENT_NODE  

  (8)輪循子節點  

  for(Node node=book.getFirstChild();node!=null;node=node.getNextSibling()){  

  if(node.getNodeType()==Node.ELEMENT_NODE){  

  if(node.getNodeName().equals("name")){

  String name=node.getNodeValue();  

  String name1=node.getFirstChild().getNodeValue();  

  System.out.println(name);  

  System.out.println(name1);  

  }  

  if(node.getNodeName().equals("price")){  

  String price=node.getFirstChild().getNodeValue();  

  System.out.println(price);

  }  

  }  

  這段代碼的列印輸出為:

  null  

  alterrjzjh  

  jjjjjj

  從上面可以看出  

  String name=node.getNodeValue();  是一個空值。而  

  String name1=node.getFirstChild().getNodeValue(); 才是真正的值,這是因為DOM把<name>rjzjh</name>也當作是兩層結構的節點.

原文 :http://www.cnblogs.com/Steveson/archive/2007/06/04/770653.html

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.