首页 > 代码库 > 对xml文件的简单解析

对xml文件的简单解析

package com.eprobj.demo;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 Xml {        public static void main(String[] args) {        //获取节点个数,以及节点属性。//        demo1();        //获取节点个数,以及节点属性, 前提是知道属性有且只有一个时候,所使用的方法。//        demo2();        ////获取节点个数,以及节点属性,以及子节点信息。        demo3();    }    private static void demo3() {        DocumentBuilderFactory  dbf = DocumentBuilderFactory.newInstance();        try {            DocumentBuilder db = dbf.newDocumentBuilder();            Document document =    db.parse("books.xml");            //通过book标签获取相应的节点            NodeList bookList = document.getElementsByTagName("book");            System.out.println("一共有"+bookList.getLength()+"本书!");            //遍历每一个book节点            for(int i=0; i<bookList.getLength(); i++){                Node book = bookList.item(i);                //获取book节点的所有属性                NamedNodeMap attrMap = book.getAttributes();                System.out.println("第"+(i+1)+"本书共有"+attrMap.getLength()+"个属性.");                //遍历book的属性                for (int j = 0; j < attrMap.getLength(); j++) {                    //属性键值对                    Node attrNode = attrMap.item(j);                    System.out.println("属性名:"+attrNode.getNodeName());                    System.out.println("属性值:"+attrNode.getNodeValue());                                        //获取节点下的所有子节点                    NodeList childList =book.getChildNodes();                    System.out.println("第"+(i+1)+"本书共有"+childList.getLength()+"子节点!");                    //遍历节点,获取节点名称                    for (int k = 0; k < childList.getLength(); k++) {                        Node childNode = childList.item(k);                        //如果当前节点是文字节点的话,只显示NodeNmae();                        if(childNode.getNodeType() == Node.ELEMENT_NODE)                        System.out.println(childList.item(k).getNodeName());                    }                }            }        } catch (ParserConfigurationException e) {            // TODO Auto-generated catch block            e.printStackTrace();        } catch (SAXException e) {            // TODO Auto-generated catch block            e.printStackTrace();        } catch (IOException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }            }    private static void demo2() {        DocumentBuilderFactory  dbf = DocumentBuilderFactory.newInstance();        try {            DocumentBuilder db = dbf.newDocumentBuilder();            Document document =    db.parse("books.xml");            //通过book标签获取相应的节点            NodeList bookList = document.getElementsByTagName("book");            System.out.println("一共有"+bookList.getLength()+"本书!");            //遍历每一个book节点            //遍历每一个book节点            for(int i=0; i<bookList.getLength(); i++){                Node book = bookList.item(i);                //获取book节点的所有属性                NamedNodeMap attrMap = book.getAttributes();                System.out.println("第"+(i+1)+"本书共有"+attrMap.getLength()+"个属性.");                //遍历book的属性                for (int j = 0; j < attrMap.getLength(); j++) {                    //属性键值对                    Node attrNode = attrMap.item(j);                    System.out.println("属性名:"+attrNode.getNodeName());                    System.out.println("属性值:"+attrNode.getNodeValue());                }            }        } catch (ParserConfigurationException e) {            // TODO Auto-generated catch block            e.printStackTrace();        } catch (SAXException e) {            // TODO Auto-generated catch block            e.printStackTrace();        } catch (IOException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }    }    private static void demo1() {        DocumentBuilderFactory  dbf = DocumentBuilderFactory.newInstance();        try {            DocumentBuilder db = dbf.newDocumentBuilder();            Document document =    db.parse("books.xml");            //通过book标签获取相应的节点            NodeList bookList = document.getElementsByTagName("book");            System.out.println("一共有"+bookList.getLength()+"本书!");                        //遍历每一个book节点            for(int i=0; i<bookList.getLength(); i++){                Node book = bookList.item(i);                //获取book节点的所有属性                NamedNodeMap attrMap = book.getAttributes();                System.out.println("第"+(i+1)+"本书共有"+attrMap.getLength()+"个属性.");                //遍历book的属性                for (int j = 0; j < attrMap.getLength(); j++) {                    //属性键值对                    Node attrNode = attrMap.item(j);                    System.out.println("属性名:"+attrNode.getNodeName());                    System.out.println("属性值:"+attrNode.getNodeValue());                                    }            }        } catch (ParserConfigurationException e) {            // TODO Auto-generated catch block            e.printStackTrace();        } catch (SAXException e) {            // TODO Auto-generated catch block            e.printStackTrace();        } catch (IOException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }    }}

 


使用了两种方式。在代码中已经体现出来。
books.xml使用的是相对路径。


<?xml version="1.0"?><bookstore><book id="1"><name>格林童话</name><author>轨迹<author><language>english</language><year>2016</year></book><book id="2"><name>格林童话</name><author>轨迹<author><language>english</language><price>20</price></book></bookstore>

 

 

 

对xml文件的简单解析