測試xml文件:java
<?xml version="1.0" encoding="UTF-8"?> dom
<RESULT> 測試
<VALUE> ui
<NO>A1234</NO> spa
<ADDR>XX號</ADDR> .net
</VALUE> xml
<VALUE> element
<NO>B1234</NO> get
<ADDR>XX組</ADDR> input
</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 {
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));
}
}
}
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)+"毫秒");
}
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毫秒