先看源码
Sax
很清楚的看出sax比dom方式快得多
<?xml version="1.0" encoding="UTF-8"?>
<Result>
<Value>
<No>A1234</No>
<Address>武汉市新洲区</Address>
</Value>
<Value>
<No>B1234</No>
<Address>武汉市洪山区</Address>
</Value>
<Value>
<No>A1234</No>
<Address>武汉市新洲区</Address>
</Value>
<Value>
<No>B1234</No>
<Address>武汉市洪山区</Address>
</Value>
</Result>
测试main方法public static void main(String[] args) {
String url = TestXmlByDom.class.getResource("").getPath();
File f = new File(url+"/NewFile.xml");
try {
//Dom解析
TestXmlByDom.resolve(f);
System.out.println("---------------------------------------");
//Sax解析
TestXmlBySax.resolve(f);
System.out.println("---------------------------------------");
//JDom解析
TestXmlByJdom.resolve(f);
System.out.println("---------------------------------------");
//Dom4J解析
TestXmlByDom4J.resolve(f);
System.out.println("---------------------------------------");
} catch (Exception e) {
e.printStackTrace();
}
}
dom
public static void resolve(File f) throws Exception {
long time1 = System.currentTimeMillis();
System.out.println("Reading By Dom!");
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(f);
NodeList l = (NodeList) doc.getElementsByTagName("Value");
for(int i=0;i<l.getLength();i++){
System.out.println("车牌号码:"+doc.getElementsByTagName("No").item(i).getFirstChild().getNodeValue());
System.out.println("车牌地址:"+doc.getElementsByTagName("Address").item(i).getFirstChild().getNodeValue());
}
long time2 = System.currentTimeMillis();
long time = time2 - time1 ;
System.out.println("运行时间为:"+time+"毫秒!");
}
Sax
Stack tags = new Stack();
public static void resolve(File f) throws Exception{
long time1 = System.currentTimeMillis();
System.out.println("Reading By Sax!");
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser sp = factory.newSAXParser();
TestXmlBySax reader = new TestXmlBySax();
sp.parse(f, reader);
long time2 = System.currentTimeMillis();
long time = time2 - time1 ;
System.out.println("运行时间为:"+time+"毫秒!");
}
@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
String tag = (String) tags.peek();
if(tag.equals("No")){
System.out.println("车牌号码:"+ new String(ch, start, length));
}
if(tag.equals("Address")){
System.out.println("车牌地址:"+ new String(ch, start, length));
}
}
@Override
public void startElement(String uri, String localName, String name,
Attributes attributes) throws SAXException {
//把此项压入栈顶
tags.push(name);
}
@Override
public void endElement(String arg0, String arg1, String arg2)
throws SAXException {
//从栈中移除此项
tags.pop();
}
测试结果:
Reading By Dom!
车牌号码:A1234
车牌地址:武汉市新洲区
车牌号码:B1234
车牌地址:武汉市洪山区
车牌号码:A1234
车牌地址:武汉市新洲区
车牌号码:B1234
车牌地址:武汉市洪山区
运行时间为:21毫秒!
---------------------------------------
Reading By Sax!
车牌号码:A1234
车牌地址:武汉市新洲区
车牌号码:B1234
车牌地址:武汉市洪山区
车牌号码:A1234
车牌地址:武汉市新洲区
车牌号码:B1234
车牌地址:武汉市洪山区
运行时间为:4毫秒!
很清楚的看出sax比dom方式快得多
如果解析很大的xml文件 sax比dom效率要好很多,这是因为dom方式会提前将整个xml加载到内存中,浪费的效率,但是如果要求修改xml或者导航某一节点的父子节点等的情况下 ,dom比sax要方便些,而sax处理类似于流,能够马上就开始处理,而不需要等待所有的数据加载完