XML文檔的DOM解析

xml的DOM解析:

<?xml version="1.0" encoding="UTF-8"?>
<books> <!--根節點--><!--這裏的空白也算一個節點,所以books共有5個子節點-->
	<book id="book1" w="wwwww">
		<name><a>AAAAAA</a>bookName1</name>
		<price>10.0</price>
	</book>
	<book id="book2">
		<name>bookName2</name>
		<author>bookAuthor2</author>
	</book>
</books>


這張圖片纔是精髓:

wKiom1WiHvnw31YxAADC7aM4nOU348.jpg


代碼:

package com.zhang.xml.dom;

import java.io.IOException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class DomParse {

	public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException {
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();  
        DocumentBuilder builder = factory.newDocumentBuilder();  
        Document document = builder.parse("books.xml");  //獲得文檔對象
        Element element = document.getDocumentElement(); //獲得根節點
        
        NodeList books = element.getElementsByTagName("book"); //得到所有book節點,2個
//      element.getChildNodes(); //得到所有子節點,5個

        System.out.println("共有" + books.getLength() + "書");
        
        for(int i=0; i<books.getLength(); i++) {
        	System.out.println("-----------------第" + (i+1) + "個節點開始解析-----------------");
        	Node bookNode = books.item(i); //得到第i個子節點
        	
        	NamedNodeMap attrs = bookNode.getAttributes(); //得到節點的所有屬性節點
        	System.out.println("---第"+ (i + 1) +"本書共有" + attrs.getLength() + "個屬性節點---");
        	for(int j=0; j<attrs.getLength(); j++) {
        		Node attr = attrs.item(j); //得到第j個屬性節點
        		String attrName = attr.getNodeName(); //節點名
        		String attrValue = attr.getNodeValue(); //節點值
        		System.out.println("------屬性名:" + attrName + "-->屬性值:" + attrValue + "------");
        	}
        	//如果知道book節點有且只有一個w屬性節點
//        	Element ele = (Element)bookNode;
//        	System.out.println(ele.getAttribute("w"));
        	
        	NodeList bookChildren = bookNode.getChildNodes(); //得到所有子節點
        	System.out.println("---第" + (i + 1) + "個節點共有" + bookChildren.getLength() +"個子節點---");
        	for(int k=0; k<bookChildren.getLength(); k++) {
        		if(bookChildren.item(k).getNodeType() ==Node.ELEMENT_NODE) {//元素節點
        			Node bookChild = bookChildren.item(k);
        			String nodeName = bookChild.getNodeName();
        			String textContent = bookChild.getTextContent(); 
//        			System.out.println(bookChild.getNodeValue()); //null
//        			System.out.println(bookChild.getFirstChild().getNodeValue()); // textContent
        			System.out.println("------第" + (k + 1) + "節點的節點名:" + nodeName + "-->節點值:" + textContent + "------");
        		}
        	}
        	
        	System.out.println("-----------------第" + (i+1) + "節點結束解析-----------------");
        	System.out.println();
        }

	}
	

}


結果:

wKiom1WiHz6ConKzAAL4CT4d_Tw043.jpg

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章