Java四种XML解析技术介绍
解析xml的技术众多,都把我搞晕呼了,今天总结了一下,罗列出来,共有四类:
dom,jdom,sax,dom4j
本人环境jdk5.0 解析目标文件test.xml放在项目的src文件下.
<?xml version="1.0" encoding="GB2312"?><RESULT> <VALUE> <NO>A1</NO> <ADDR>四川省</ADDR> </VALUE> <VALUE> <NO>B2</NO> <ADDR>湖北省</ADDR> </VALUE></RESULT>
package com.test;import java.io.File;import javax.xml.parsers.DocumentBuilder;import javax.xml.parsers.DocumentBuilderFactory;import org.w3c.dom.Document;import org.w3c.dom.NodeList;public class DomParser { public static void main(String arge[]) { long lasting = System.currentTimeMillis(); try { File f = new File("src/test.xml"); DocumentBuilderFactory factory = DocumentBuilderFactory .newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document doc = builder.parse(f); 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()); } } catch (Exception e) { e.printStackTrace(); } System.out.println("运行时间:" + (System.currentTimeMillis() - lasting) + " 毫秒"); }}
package com.test;import javax.xml.parsers.SAXParser;import javax.xml.parsers.SAXParserFactory;import org.xml.sax.Attributes;import org.xml.sax.InputSource;import org.xml.sax.SAXException;import org.xml.sax.helpers.DefaultHandler;public class SaxParser extends DefaultHandler { java.util.Stack<String> tags = new java.util.Stack<String>(); public SaxParser() { super(); } public static void main(String args[]) { long lasting = System.currentTimeMillis(); try { SAXParserFactory sf = SAXParserFactory.newInstance(); SAXParser sp = sf.newSAXParser(); SaxParser reader = new SaxParser(); sp.parse(new InputSource("src/test.xml"), reader); } catch (Exception e) { e.printStackTrace(); } System.out.println("运行时间:" + (System.currentTimeMillis() - lasting) + " 毫秒"); } 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 startElement(String uri, String localName, String qName, Attributes attrs) { tags.push(qName); } @Override public void endElement(String uri, String localName, String name) throws SAXException { tags.pop(); }}
package com.test;import java.io.File;import java.util.List;import org.jdom.Document;import org.jdom.Element;import org.jdom.input.SAXBuilder;public class JdomParser { public static void main(String arge[]) { long lasting = System.currentTimeMillis(); try { SAXBuilder builder = new SAXBuilder(); Document doc = builder.build(new File("src/test.xml")); Element foo = doc.getRootElement(); List<Element> allChildren = foo.getChildren(); for (int i = 0; i < allChildren.size(); i++) { System.out.print("号码:" + allChildren.get(i).getChild("NO").getText()); System.out.println("地址:" + allChildren.get(i).getChild("ADDR").getText()); } } catch (Exception e) { e.printStackTrace(); } System.out.println("运行时间:" + (System.currentTimeMillis() - lasting) + " 毫秒"); }}
package com.test;import java.io.File;import java.util.Iterator;import org.dom4j.Document;import org.dom4j.Element;import org.dom4j.io.SAXReader;public class Dom4jParser { public static void main(String arge[]) { long lasting = System.currentTimeMillis(); try { File f = new File("src/test.xml"); SAXReader reader = new SAXReader(); Document doc = reader.read(f); Element root = doc.getRootElement(); Element foo; for (Iterator i = root.elementIterator("VALUE"); i.hasNext();) { foo = (Element) i.next(); System.out.print("号码:" + foo.elementText("NO")); System.out.println("地址:" + foo.elementText("ADDR")); } } catch (Exception e) { e.printStackTrace(); } System.out.println("运行时间:" + (System.currentTimeMillis() - lasting) + " 毫秒"); }}