DOM、SAX、JDOM DOM、JDOM SAX、DOM4J DOM、DOM4J SAX 性能比对

测试xml文件:

<?xml version="1.0" encoding="UTF-8"?> 

<RESULT> 

<VALUE> 

   <NO>A1234</NO> 

   <ADDR>XX号</ADDR> 

</VALUE> 

<VALUE> 

   <NO>B1234</NO> 

   <ADDR>XX组</ADDR> 

</VALUE> 

</RESULT>

测试代码:

import java.io.IOException;

import java.io.InputStream;

import java.util.Iterator;

import java.util.List;

import javax.xml.parsers.DocumentBuilder;

import javax.xml.parsers.DocumentBuilderFactory;

import javax.xml.parsers.ParserConfigurationException;

import javax.xml.parsers.SAXParser;

import javax.xml.parsers.SAXParserFactory;

import org.dom4j.Document;

import org.dom4j.DocumentException;

import org.dom4j.io.DOMReader;

import org.dom4j.io.SAXReader;

import org.jdom.Element;

import org.jdom.input.DOMBuilder;

import org.jdom.input.SAXBuilder;

import org.junit.Test;

import org.xml.sax.Attributes;

import org.xml.sax.SAXException;

import org.xml.sax.helpers.DefaultHandler;

public class XMLParserTest {

    @Test

    public void testDOMTypeParseXML() throws ParserConfigurationException, SAXException,

            IOException {

        long lasting =System.currentTimeMillis(); 

        InputStream is = XMLParserTest.class.getClassLoader().getResourceAsStream("test.xml");

        DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance(); 

        DocumentBuilder builder=factory.newDocumentBuilder(); 

        org.w3c.dom.Document doc = builder.parse(is); 

        org.w3c.dom.NodeList nl = doc.getElementsByTagName("VALUE"); 

        for (int i=0;i<nl.getLength();i++){ 

//            System.out.print("车牌号码:" + doc.getElementsByTagName("NO").item(i).getFirstChild().getNodeValue()); 

//            System.out.println("车主地址:" + doc.getElementsByTagName("ADDR").item(i).getFirstChild().getNodeValue());

        }

        long end =  System.currentTimeMillis();

        System.out.println("DOM TYPE PARSE COST "+(end - lasting)+"毫秒");

    }

    public class MyXMLReader extends DefaultHandler{

        @SuppressWarnings("rawtypes")

        java.util.Stack tags = new java.util.Stack(); 

        public MyXMLReader() { 

            super(); 

        } 

        @SuppressWarnings("unchecked")

        public void startElement(String uri,String localName,String qName,Attributes attrs) { 

            tags.push(qName);

        } 

        public void characters(char ch[], int start, int length) throws SAXException { 

            String tag = (String) tags.peek(); 

            if (tag.equals("NO")) { 

//                System.out.print("车牌号码:" + new String(ch, start, length)); 

            } 

            if (tag.equals("ADDR")) { 

//                System.out.println("地址:" + new String(ch, start, length)); 

            } 

        }

    }

    @Test

    public void testSAXTypeParseXML() throws SAXException, IOException, ParserConfigurationException{

        long lasting = System.currentTimeMillis(); 

        InputStream is = XMLParserTest.class.getClassLoader().getResourceAsStream("test.xml");

        SAXParserFactory sf = SAXParserFactory.newInstance(); 

        SAXParser sp = sf.newSAXParser(); 

        MyXMLReader me = new MyXMLReader();

        sp.parse(is, me); 

        System.out.println("SAX TYPE PARSE COST "+(System.currentTimeMillis() - lasting)+"毫秒");

    }

    @Test

    public void testJDOMSAXTypeParseXML(){

        long lasting = System.currentTimeMillis(); 

        try { 

            InputStream is = XMLParserTest.class.getClassLoader().getResourceAsStream("test.xml");

            SAXBuilder builder = new SAXBuilder(); 

            org.jdom.Document doc = builder.build(is); 

            Element foo = doc.getRootElement(); 

            List allChildren = foo.getChildren(); 

            for(int i=0;i < allChildren.size();i++) { 

//                System.out.print("车牌号码:" + ((Element)allChildren.get(i)).getChild("NO").getText()); 

//                System.out.println("车主地址:" + ((Element)allChildren.get(i)).getChild("ADDR").getText()); 

            } 

        } catch (Exception e) { 

            e.printStackTrace(); 

        }finally{

            System.out.println("JDOM SAX TYPE PARSE COST "+(System.currentTimeMillis() - lasting)+"毫秒");

        }

    }

    @Test

    public void testJDOMDOMTypeParseXML(){

        long lasting = System.currentTimeMillis(); 

        try { 

            InputStream is = XMLParserTest.class.getClassLoader().getResourceAsStream("test.xml");

            DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance(); 

            DocumentBuilder builder=factory.newDocumentBuilder(); 

            org.w3c.dom.Document document = builder.parse(is);

            DOMBuilder builder1 = new DOMBuilder(); 

            org.jdom.Document doc = builder1.build(document); 

            Element foo = doc.getRootElement(); 

            List allChildren = foo.getChildren(); 

            for(int i=0;i < allChildren.size();i++) { 

//                System.out.print("车牌号码:" + ((Element)allChildren.get(i)).getChild("NO").getText()); 

//                System.out.println("车主地址:" + ((Element)allChildren.get(i)).getChild("ADDR").getText()); 

            } 

        } catch (Exception e) { 

            e.printStackTrace(); 

        }finally{

            System.out.println("JDOM DOM TYPE PARSE COST "+(System.currentTimeMillis() - lasting)+"毫秒");

        }

    }

    @Test

    public void testDOM4JSAXTypeParserXML() throws DocumentException{

        long lasting = System.currentTimeMillis(); 

        InputStream is = XMLParserTest.class.getClassLoader().getResourceAsStream("test.xml");

        SAXReader reader = new SAXReader(); 

        Document doc = reader.read(is); 

        org.dom4j.Element root = doc.getRootElement(); 

        org.dom4j.Element foo; 

        for (Iterator i = root.elementIterator("VALUE");i.hasNext();){

            foo = (org.dom4j.Element)i.next(); 

//            System.out.print("车牌号码:" + foo.elementText("NO")); 

//            System.out.println("车主地址:" + foo.elementText("ADDR")); 

        } 

        System.out.println("DOM4J SAX TYPE PARSE COST "+(System.currentTimeMillis() - lasting)+"毫秒");

    }

    @Test

    public void testDOM4JDOMTypeParserXML() throws DocumentException, ParserConfigurationException, SAXException, IOException{

        long lasting = System.currentTimeMillis(); 

        InputStream is = XMLParserTest.class.getClassLoader().getResourceAsStream("test.xml");

        DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance(); 

        DocumentBuilder builder=factory.newDocumentBuilder(); 

        org.w3c.dom.Document document = builder.parse(is); 

        DOMReader reader = new DOMReader();

        Document doc = reader.read(document); 

        org.dom4j.Element root = doc.getRootElement(); 

        org.dom4j.Element foo; 

        for (Iterator i = root.elementIterator("VALUE");i.hasNext();){

            foo = (org.dom4j.Element)i.next(); 

//            System.out.print("车牌号码:" + foo.elementText("NO")); 

//            System.out.println("车主地址:" + foo.elementText("ADDR")); 

        } 

        System.out.println("DOM4J DOM TYPE PARSE COST "+(System.currentTimeMillis() - lasting)+"毫秒");

    }

}

结论:

当测试xml文件大小为50kb左右时:

DOM TYPE PARSE COST 144毫秒

SAX TYPE PARSE COST 43毫秒

JDOM SAX TYPE PARSE COST 179毫秒

JDOM DOM TYPE PARSE COST 145毫秒

DOM4J SAX TYPE PARSE COST 128毫秒

DOM4J DOM TYPE PARSE COST 106毫秒


你可能感兴趣的:(java,xml,sax,dom)