基於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處理類似於流,能夠馬上就開始處理,而不需要等待所有的數據加載完


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章