閒來無事,看如何用DOM解析XML

//在下純給自己做筆記,抱歉。
<?xml version="1.0" encoding="UTF-8"?>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<bookstore>
    <book id="1">
        <name><a>aa</a>冰與火之歌</name>
        <author>喬治馬丁</author>
        <year>2014</year>
        <price>89</price>
    </book>
    <book id="2">
        <name>安徒生童話</name>
        <year>2004</year>
        <price>77</price>
        <language>English</language>
    </book>

</bookstore>
    
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package domtest;

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;

/**
 *
 * @author Administrator
 */
public class DomTest {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException {
        // TODO code application logic here
        DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
        DocumentBuilder db=dbf.newDocumentBuilder();
        Document document=db.parse("newXMLDocument.xml");
        //獲取所有book節點的集合
        NodeList bookList=document.getElementsByTagName("book");
        System.out.println(bookList.getLength());
        //遍歷每一個book節點
        for(int i=0;i<bookList.getLength();i++){
            System.out.println("下面開始遍歷第"+(i+1)+"本書的內容");
            //通過獲取一個book節點
            Node book=bookList.item(i);
            //獲取book節點的所有屬性的集合
            NamedNodeMap attrs=book.getAttributes();
            System.out.println("第"+(i+1)+"本書共有"+attrs.getLength()+"個屬性");
            for(int j=0;j<attrs.getLength();j++){
                //通過item()方法獲取某一個屬性
                Node attr=attrs.item(j);
                //獲取屬性名
                System.out.println("屬性名:"+attr.getNodeName());
                //獲取屬性值
                System.out.println("屬性值:"+attr.getNodeValue());
            }
            /*Element book=(Element)bookList.item(i);
            String attrValue=book.getAttribute("id");
            System.out.println("id屬性的屬性值爲:"+attrValue);*/
            NodeList childNodes=book.getChildNodes();
            //遍歷childNodes獲取每個節點的節點名和節點值
            System.out.println("第"+(i+1)+"本書共有"+childNodes.getLength()+"個子節點");
            for(int k=0;k<childNodes.getLength();k++){
                //區分出text類型的node以及element類型的node
                if(childNodes.item(k).getNodeType()==Node.ELEMENT_NODE){
                     System.out.println("第"+(k+1)+"個節點名:"+childNodes.item(k).getNodeName());
                     System.out.println("第"+(k+1)+"個節點的節點值:"+childNodes.item(k).getTextContent());
                }
               
            }
            System.out.println("======================結束遍歷第"+(i+1)+"本書==========================");
        }
    }
    
}


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