基于jdk的2种xml解析方式的比较

先看源码
<?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处理类似于流,能够马上就开始处理,而不需要等待所有的数据加载完


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值