使用DOM解析XML的文件屬性名和屬性值、節點名和節點值

一、新建一個demo.xml的文件:

<? xml version="1.0" encoding="UTF-8" ?>
<bookstore>
  <book id="1">
     <name>放學後</name>
     <author>cmirssd</author>
     <year>2010</year>
     <price>25</price>
  </book>

  <book id="2">
     <name>你的孤獨,雖敗猶榮</name>
     <author>劉同</author>
     <year>2011</year>
     <price>30</price>
  </book>
</bookstore>

使用DOM的準備工作:

1、創建DocumentBuilderFactory對象(newInstance方法)

2、創建DocumentBuilder對象(newDocumentBuilder方法)

3、DocumentBuilder對象的parse()方法


新建一個Xmltest.java,代碼如下:

package com.imooc.io;

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.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class Xmltest {
	public static void main(String[] args){
		Xmltest t = new Xmltest();
		t.readXML();
	}
	
	public void readXML(){
		//創建DocumentBuilderFactory對象
				DocumentBuilderFactory a = DocumentBuilderFactory.newInstance();
				try {
					//創建DocumentBuilder對象
					DocumentBuilder b = a.newDocumentBuilder();
					//通過DocumentBuilder對象的parse方法返回一個Document對象
					Document document = b.parse("demo.xml");
					//通過Document對象的getElementsByTagName()返根節點的一個list集合
					NodeList booklist = document.getElementsByTagName("book");
					for(int i =0; i<booklist.getLength(); i++){
						//循環遍歷獲取每一個book
						Node book = booklist.item(i);
						//通過Node對象的getAttributes()方法獲取全的屬性值
						NamedNodeMap bookmap = book.getAttributes();
						//循環遍每一個book的屬性值
					    for(int j = 0; j<bookmap.getLength(); j++){
					    	Node node = bookmap.item(j);
					    	//通過Node對象的getNodeName()和getNodeValue()方法獲取屬性名和屬性值
					    	System.out.println(node.getNodeName());
					    	System.out.println(node.getNodeValue());
					    }
					    NodeList childlist = book.getChildNodes();
					    for(int t = 0; t<childlist.getLength(); t++){
					    	//區分出text類型的node以及element類型的node
					    	if(childlist.item(t).getNodeType() == Node.ELEMENT_NODE){
					    		System.out.println(childlist.item(t).getNodeName());
					    		//如果子節點裏面還嵌套着子節點,如:<name><a>www</a>放學後</name>
					    		//getTextContent()方法能夠一起輸出:www放學後
					    		System.out.println(childlist.item(t).getTextContent());
					    		//getFirstChild()的getNodeValue(),如果子節點裏面嵌套這子節點
					    		//如:<name><a>www</a>放學後</name>,則會輸出null
					    		//System.out.println(childlist.item(t).getFirstChild().getNodeValue());
					    	}
					    }
					}
				} catch (ParserConfigurationException e) {
					e.printStackTrace();
				} catch (SAXException e) {
					e.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();
				}
	}

}



其中獲取子節點值得方法有:getTextContent()和getFirstChild().getNodeValue()方法。

區別是:

1、如果子節點裏面還嵌套着子節點,如:<name><a>www</a>放學後</name>,getTextContent()方法能夠一起輸出:www放學後

2、如果子節點裏面嵌套這子節點,如:<name><a>www</a>放學後</name>,則會輸出null

運行結果:

id
1
name
放學後
author
lcb
year
2010
price
25
id
2
name
你的孤獨,雖敗猶榮
author
劉同
year
2011
price
30

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