JAva使用DOM讀取XML數據(解析)


原來一切都是有套路的

使用DOM解析XML文檔步驟

1、創建解析器工廠對象 DocumentBuildFactory對象

2、由解析器工廠對象創建解析器對象,即DocumentBuilder對象

3、由解析器對象對指定XML文件進行解析,構建相應的DOM樹,創建Document對象,生成一個Document對象

4、以Document對象為起點對DOM樹的節點進行查詢

5、使用Document的getElementsByTagName方法獲取元素名稱,生成一個NodeList集合,

6、遍歷集合

實例代碼如下:

package d0620;

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

/*
 * 解析phones.xml文件
 */
public class ParseXML {
    public static void main(String[] arg){
        //1、創建解析器工廠對象 DocumentBuildFactory對象
        DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
        DocumentBuilder db=null;
        Document dom=null;
        try {
            //2、由解析器工廠對象創建解析器對象,即DocumentBuilder對象
            db=dbf.newDocumentBuilder();
            //3、由解析器對象對指定XML文件進行解析,構建相應的DOM樹,創建Document對象,生成一個Document對象
            dom=db.parse("phones.xml");
            //4、以Document對象為起點對DOM樹的節點進行查詢
            //得到所有brand節點信息,是個集合
            /*
             * getElementsByTagName(String tagname) 
          按文檔順序返回包含在文檔中且具有給定標記名稱的所有 Element 的 NodeList。
             */
            NodeList brandlist=dom.getElementsByTagName("Brand");
            //遍歷所有brand
            for(int i=0;i<brandlist.getLength();i++){
                Node brand=brandlist.item(i);
                //轉成元素,獲取brand的元素值
                Element brandelement=(Element)brand;
                String brandname=brandelement.getAttribute("name");
                System.out.println(brandname);
                //查找Type
                NodeList typelist=brandelement.getChildNodes();
                for(int j=0;j<typelist.getLength();j++){
                    Node typenode=typelist.item(j);
                    if(typelist.item(j) instanceof Element){
                        Element typename=(Element)typenode;
                        String typen=typename.getAttribute("name");
                        System.out.println("\t"+typen);
                    }
                    
                }
            }
        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        } catch (SAXException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            System.out.println("解析結束");
        }
        
    }
}

 特別注意:

 if(typelist.item(j) instanceof Element){
                        Element typename=(Element)typenode;
                        String typen=typename.getAttribute("name");
                        System.out.println("\t"+typen);
                    }

如果不加判斷,讀取子節點時候,會把第一個和第二個和下一個換行的一些空格當作文本來處理,強制轉換就會出錯

Exception in thread "main" java.lang.ClassCastException: com.sun.org.apache.xerces.internal.dom.DeferredTextImpl cannot be cast to org.w3c.dom.Element
    at d0620.ParseXML.main(ParseXML.java:47)

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM