解析XML的四种方式

来源:互联网 发布:手机选购 知乎 编辑:程序博客网 时间:2024/06/05 14:09
解析xml:
<?xml version="1.0" encoding="UTF-8"?>
<bookstore>
    <book id="1">
        <name>冰与火之歌</name>
        <author>乔治马丁</author>
        <year>2014</year>
        <price>89</price>
    </book>
    <book id="2">
        <name>安徒生童话</name>
        <year>2004</year>
        <price>34</price>
        <language>English</language>
    </book>
</bookstore>  
第一种DOM方式解析
public static void main(String[] args) {
        //创建一个DocumentBuiderFactory的对象
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        try {
            //创建一个DocumentBuider的对象
            DocumentBuilder db = dbf.newDocumentBuilder();
            //通过DocumentBuider对象的parse方法加载books.xml文件到项目下
            Document document = db.parse("books.xml");
            //获取所有book节点的集合
            NodeList bookList = document.getElementsByTagName("book");
            //遍历每一个book节点
            for(int i=0;i<bookList.getLength();i++){
                //不知道属性节点和属性名称方法
                //通过 item(i)获取一个book节点,nodeList的索引中从0开始
                Node book = bookList.item(i);
                //获取book节点所有属性的集合
                NamedNodeMap arrts = book.getAttributes();
                //遍历book的属性
                for(int j=0;j<arrts.getLength();j++){
                    //通过item(index)方法获取book节点的某一个属性
                    Node attr =  arrts.item(j);
                    //获取属性名
                    attr.getNodeName();
                    //属性值
                    attr.getNodeValue();
                }
                /*已知属性节点和属性名称的方法
                Element book = (Element)bookList.item(i);
                String attrValue = book.getAttribute("id");
                System.out.println(attrValue);*/
                //获取book节点的子节点
                NodeList childNodes = book.getChildNodes();
                //遍历childNodes获取每个节点的名称和节点值
                for(int k=0;k<childNodes.getLength();k++){
                    //区分text类型的node和Element类型的node
                    if(childNodes.item(k).getNodeType() == Node.ELEMENT_NODE){
                        //获取了element类型节点的节点名称
                        childNodes.item(k).getNodeName();
                        //获取了element类型节点的节点值
                        //childNodes.item(k).getFirstChild().getNodeValue();
                        //可以获取节点中包含子节点的内容和当前内容
                        childNodes.item(k).getTextContent();
                    }
                }
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }  
第二种SAX方式解析,总共两个类,第一个是我们包含main方法的主要是获得实例和解析对象,第二个是我们的handler继承自我们的defaultHandler
package handler;
import java.util.ArrayList;
import javax.xml.stream.events.StartElement;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
import entity.book;
public class SAXParserHandler extends DefaultHandler{
    
     public int bookIndex=0;
     String value = null;
     book  book = null;
    private  ArrayList<book> bookList = new ArrayList<book>();
    
    public ArrayList<book> getBookList() {
        return bookList;
    }
    /**
     * 用来标识解析开始
     */
    @Override
    public void startDocument() throws SAXException {
        // TODO Auto-generated method stub
        super.startDocument();
        System.out.println("SAX解析开始");
    }
    
    /**
     * 用来标识解析结束
     */
    @Override
    public void endDocument() throws SAXException {
        // TODO Auto-generated method stub
        super.endDocument();
        System.out.println("SAX解析结束");
    }
    
    /**
     * 用来解析xml元素
     */
    @Override
    public void startElement(String uri, String localName, String qName,
            Attributes attributes) throws SAXException {
        // TODO Auto-generated method stub
        super.startElement(uri, localName, qName, attributes);
        //开始解析book元素的属性
        if(qName.equals("book")){
            //new一个book对象
            book = new book();
            bookIndex++;
            System.out.println("=====================开始遍历第"+bookIndex+"本书================================");
            /*//已知book下元素属性名称,根据属性名称获取属性值
            String value = attributes.getValue("id");
            System.out.println("book的属性值是:"+value);*/
            //不知道到book下属性名称和个数,如何获取属性名和值呢
            int num = attributes.getLength();
            for(int i=0;i<num;i++){
                System.out.println("book元素的第"+i+1+"个属性名称为:"+attributes.getQName(i));
                System.out.println("book元素的第"+i+1+"个属性值为:"+attributes.getValue(i));
                if(attributes.getQName(i).equals("id")){
                    book.setId(attributes.getValue(i));
                }
            }
        }else if(!qName.equals("book")&&!qName.equals("bookstore")){
            System.out.print("节点名是:"+qName);
        }
        
    }
    
    /**
     * 用来解析xml的结束标签
     */
    @Override
    public void endElement(String uri, String localName, String qName)
            throws SAXException {
        // TODO Auto-generated method stub
        super.endElement(uri, localName, qName);
        if(qName.equals("book")){
            bookList.add(book);
            book = null;
            System.out.println("=====================结束遍历第"+bookIndex+"本书================================");
        }else if(qName.equals("name")){
            book.setName(value);
        }else if(qName.equals("author")){
            book.setAuthor(value);
        }else if(qName.equals("year")){
            book.setYear(value);
        }else if(qName.equals("price")){
            book.setPrice(value);
        }else if(qName.equals("language")){
            book.setLanguage(value);
        }
    }
    
    @Override
    public void characters(char[] ch, int start, int length)
            throws SAXException {
        // TODO Auto-generated method stub
        super.characters(ch, start, length);
        value = new String(ch, start, length);
        if(!value.trim().equals("")){
            System.out.println(value);
        }
    }
}

package xml;
import java.io.IOException;
import handler.SAXParserHandler;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.SAXException;
import entity.book;
public class SAXTest {
    public static void main(String[] args) {
        //获取SAXParserFactory的实例
        SAXParserFactory factory = SAXParserFactory.newInstance();
        //通过factory获取SAXParser实例
        try {
            SAXParser parser = factory.newSAXParser();
            SAXParserHandler handler = new SAXParserHandler();
            parser.parse("books.xml", handler);
            handler.getBookList().size();
            System.out.println("共有:"+handler.getBookList().size()+"本书");
            for(book book:handler.getBookList()){
                System.out.println(book.getId());
                System.out.println(book.getName());
                System.out.println(book.getAuthor());
                System.out.println(book.getYear());
                System.out.println(book.getPrice());
                System.out.println(book.getLanguage());
                System.out.println("-=====finish-======");
                
            }
        } 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();
        }
    }
}

package entity;
public class book {
    private String id;
    private String name;
    private String year;
    private String price;
    private String language;
    private String author;
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getYear() {
        return year;
    }
    public void setYear(String year) {
        this.year = year;
    }
    public String getPrice() {
        return price;
    }
    public void setPrice(String price) {
        this.price = price;
    }
    public String getLanguage() {
        return language;
    }
    public void setLanguage(String language) {
        this.language = language;
    }
    public String getAuthor() {
        return author;
    }
    public void setAuthor(String author) {
        this.author = author;
    }
}
第三种方法JDOM解析, 用到的book类和上面一致
package xml;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import org.jdom.Attribute;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import entity.book;
public class JDOMTest {
    
    private static  ArrayList<book> bookListAll  = new ArrayList<book>();
    public static void main(String[] args) {
        //1.创建SAXBuilder的实例
        SAXBuilder saxBuilder = new SAXBuilder();
        InputStream in;
        try {
            //2.创建一个输入流,将xml文件加载到输入流中
            in = new FileInputStream("src/res/books.xml");
            //处理乱码
            InputStreamReader isr = new InputStreamReader(in,"UTF-8");
            //3.通过SAXBuilder的build方法,将输入流加载到SAXBuilder中
            Document document = saxBuilder.build(isr);
            //4.通过document对象获取xml文件的根节点
            Element rootElement = document.getRootElement();
            //5.获取根节点下的子节点的List集合
            List<Element> bookList = rootElement.getChildren();
            //继续进行解析
            for(Element book : bookList){
                book entityBook = new book();
                System.out.println("============开始解析第"+(bookList.indexOf(book)+1)+"书===========");
                //解析book的属性
                List<Attribute> attrList = book.getAttributes();
                //知道节点下属性名称的方式
                //book.getAttributeValue("id");
                //遍历attrList(针对不清楚book节点下属性的名字及数量)
                for(Attribute attr:attrList){
                    //获取属性名
                    String attrName = attr.getName();
                    //获取属性值
                    String attrValue = attr.getValue();
                    System.out.println("属性名:"+attrName+";属性值:"+attrValue);
                    if(attrName.equals("id")){
                        entityBook.setId(attrValue);
                    }
                }
                //对book节点的子节点的节点名以及节点值
                List<Element> bookChild = book.getChildren();
                for(Element child:bookChild){
                    System.out.println("节点名:"+child.getName()+";节点值:"+child.getValue());
                    if(child.getName().equals("name")){
                        entityBook.setName(child.getValue());
                    }else if(child.getName().equals("author")){
                        entityBook.setAuthor(child.getValue());
                    }else if(child.getName().equals("price")){
                        entityBook.setPrice(child.getValue());
                    }else if(child.getName().equals("year")){
                        entityBook.setYear(child.getValue());
                    }else if(child.getName().equals("language")){
                        entityBook.setLanguage(child.getValue());
                    }
                }
                System.out.println("============结束解析第"+(bookList.indexOf(book)+1)+"书===========");
                bookListAll.add(entityBook);
                entityBook = null;
                System.out.println(bookListAll.size());
                System.out.println(bookListAll.get(0).getAuthor());
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (JDOMException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
第四种解析DOM4j
package xml;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import entity.book;
public class DOM4JTest {
    private static ArrayList<book> bookList = new ArrayList<book>();
    /**
     * @param args
     */
    public static void main(String[] args) {
        //创建一个SAXReader的对象
        SAXReader reader = new SAXReader();
        try {
            //通过reader对象的read方法加载books.xml文件,获取document对象
            Document document = reader.read("src/res/books.xml");
            //通过document对象获取根节点bookstore
            Element bookStore = document.getRootElement();
            //通过element对象的elementIterator方法获取迭代器
            Iterator it = bookStore.elementIterator();
            //遍历迭代器,获得根节点中的信息
            while(it.hasNext()){
                book bookEntity = new book();
                System.out.println("=======开始遍历某一本书===========");
                Element book = (Element)it.next();
                //获取book的book属性名以及属性值
                List<Attribute> bookAttrs = book.attributes();
                for(Attribute attr:bookAttrs){
                    System.out.println("属性名:"+attr.getName()+";属性值:"+attr.getValue());
                    if(attr.getName().equals("id")){
                        bookEntity.setId(attr.getValue());
                    }
                }
                Iterator itt = book.elementIterator();
                while(itt.hasNext()){
                    Element bookChild = (Element)itt.next();
                    System.out.println("节点名:"+bookChild.getName()+";节点值:"+bookChild.getStringValue());
                    if(bookChild.getName().equals("name")){
                        bookEntity.setName(bookChild.getStringValue());
                    }else if(bookChild.getName().equals("author")){
                        bookEntity.setAuthor(bookChild.getStringValue());
                    }else if(bookChild.getName().equals("year")){
                        bookEntity.setYear(bookChild.getStringValue());
                    }else if(bookChild.getName().equals("price")){
                        bookEntity.setPrice(bookChild.getStringValue());
                    }else if(bookChild.getName().equals("language")){
                        bookEntity.setLanguage(bookChild.getStringValue());
                    }
                }
                System.out.println("=======结束遍历某一本书===========");
                bookList.add(bookEntity);
                bookEntity=null;
            }
            System.out.println(bookList.size());
            System.out.println(bookList.get(0).getAuthor());
        } catch (DocumentException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}
4种解析方式:
    基础方法:DOM,SAX
    扩展方法:JDOM,DOM4J
DOM解析:一次性加载xml文件
SAX解析:基于事件的一种解析,逐条语句解析,没走一句,就去判断,先去执行startDocument方法,然后再执行starElement还是endElement,最后执行endDocument方法
DOM优点:形成了树结构,直观好理解,代码更易编写;解析过程中树结构保留在内存中,方便修改
DOM缺点:当xml文件较大时,对内存耗费比较大,容易影响解析性能并造成内存溢出
SAX优点:采用事件驱动模式,对内存耗费比较小;适用于只需要处理xml中数据时。
SAX缺点:不易编码;很难同时访问同意xml中多处不同的对象。
JDOM:仅使用具体类而不适用接口,API大量使用了Collections类
DOM4J:  JDOM的一种智能分支,它合并了许多超出基本xml文档的功能
        DOM4J使用接口和抽象基本类方法,是一个优秀的JAVA xml API
        具有性能优异,灵活性好,功能强大和极端易用使用的特点
        是一个开放源码
















0 0
原创粉丝点击