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)