解析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 >
第一种:dom方式
< RESULT >
< VALUE >
< NO > A1 </ NO >
< ADDR > 四川省 </ ADDR >
</ VALUE >
< VALUE >
< NO > B2 </ NO >
< ADDR > 湖北省 </ ADDR >
</ VALUE >
</ RESULT >
1
package
com.test;
2
3 import java.io.File;
4
5 import javax.xml.parsers.DocumentBuilder;
6 import javax.xml.parsers.DocumentBuilderFactory;
7
8 import org.w3c.dom.Document;
9 import org.w3c.dom.NodeList;
10
11 public class DomParser {
12
13 public static void main(String arge[]) {
14 long lasting = System.currentTimeMillis();
15 try {
16 File f = new File("src/test.xml");
17 DocumentBuilderFactory factory = DocumentBuilderFactory
18 .newInstance();
19 DocumentBuilder builder = factory.newDocumentBuilder();
20 Document doc = builder.parse(f);
21 NodeList nl = doc.getElementsByTagName("VALUE");
22 for (int i = 0; i < nl.getLength(); i++) {
23 System.out.print("编号:"
24 + doc.getElementsByTagName("NO").item(i)
25 .getFirstChild().getNodeValue());
26 System.out.println("地址:"
27 + doc.getElementsByTagName("ADDR").item(i)
28 .getFirstChild().getNodeValue());
29 }
30 } catch (Exception e) {
31 e.printStackTrace();
32 }
33 System.out.println("运行时间:" + (System.currentTimeMillis() - lasting)
34 + " 毫秒");
35 }
36}
37
特点:不需要额外jar包支持,基于树,需要加载完整个dom结构,使用简单。
2
3 import java.io.File;
4
5 import javax.xml.parsers.DocumentBuilder;
6 import javax.xml.parsers.DocumentBuilderFactory;
7
8 import org.w3c.dom.Document;
9 import org.w3c.dom.NodeList;
10
11 public class DomParser {
12
13 public static void main(String arge[]) {
14 long lasting = System.currentTimeMillis();
15 try {
16 File f = new File("src/test.xml");
17 DocumentBuilderFactory factory = DocumentBuilderFactory
18 .newInstance();
19 DocumentBuilder builder = factory.newDocumentBuilder();
20 Document doc = builder.parse(f);
21 NodeList nl = doc.getElementsByTagName("VALUE");
22 for (int i = 0; i < nl.getLength(); i++) {
23 System.out.print("编号:"
24 + doc.getElementsByTagName("NO").item(i)
25 .getFirstChild().getNodeValue());
26 System.out.println("地址:"
27 + doc.getElementsByTagName("ADDR").item(i)
28 .getFirstChild().getNodeValue());
29 }
30 } catch (Exception e) {
31 e.printStackTrace();
32 }
33 System.out.println("运行时间:" + (System.currentTimeMillis() - lasting)
34 + " 毫秒");
35 }
36}
37
第二种:sax方式
1
package
com.test;
2
3 import javax.xml.parsers.SAXParser;
4 import javax.xml.parsers.SAXParserFactory;
5
6 import org.xml.sax.Attributes;
7 import org.xml.sax.InputSource;
8 import org.xml.sax.SAXException;
9 import org.xml.sax.helpers.DefaultHandler;
10
11 public class SaxParser extends DefaultHandler {
12 java.util.Stack<String> tags = new java.util.Stack<String>();
13
14 public SaxParser() {
15 super();
16 }
17
18 public static void main(String args[]) {
19 long lasting = System.currentTimeMillis();
20 try {
21 SAXParserFactory sf = SAXParserFactory.newInstance();
22 SAXParser sp = sf.newSAXParser();
23 SaxParser reader = new SaxParser();
24 sp.parse(new InputSource("src/test.xml"), reader);
25 } catch (Exception e) {
26 e.printStackTrace();
27 }
28 System.out.println("运行时间:" + (System.currentTimeMillis() - lasting)
29 + " 毫秒");
30 }
31
32 public void characters(char ch[], int start, int length)
33 throws SAXException {
34 String tag = (String) tags.peek();
35 if (tag.equals("NO")) {
36 System.out.print("号码:" + new String(ch, start, length));
37 }
38 if (tag.equals("ADDR")) {
39 System.out.println("地址:" + new String(ch, start, length));
40 }
41 }
42
43 public void startElement(String uri, String localName, String qName,
44 Attributes attrs) {
45 tags.push(qName);
46 }
47
48 @Override
49 public void endElement(String uri, String localName, String name)
50 throws SAXException {
51 tags.pop();
52 }
53
54}
55
特点:也不需要引入其他jar包,基于事件机制,节省内存占用,适合处理大型文档。
2
3 import javax.xml.parsers.SAXParser;
4 import javax.xml.parsers.SAXParserFactory;
5
6 import org.xml.sax.Attributes;
7 import org.xml.sax.InputSource;
8 import org.xml.sax.SAXException;
9 import org.xml.sax.helpers.DefaultHandler;
10
11 public class SaxParser extends DefaultHandler {
12 java.util.Stack<String> tags = new java.util.Stack<String>();
13
14 public SaxParser() {
15 super();
16 }
17
18 public static void main(String args[]) {
19 long lasting = System.currentTimeMillis();
20 try {
21 SAXParserFactory sf = SAXParserFactory.newInstance();
22 SAXParser sp = sf.newSAXParser();
23 SaxParser reader = new SaxParser();
24 sp.parse(new InputSource("src/test.xml"), reader);
25 } catch (Exception e) {
26 e.printStackTrace();
27 }
28 System.out.println("运行时间:" + (System.currentTimeMillis() - lasting)
29 + " 毫秒");
30 }
31
32 public void characters(char ch[], int start, int length)
33 throws SAXException {
34 String tag = (String) tags.peek();
35 if (tag.equals("NO")) {
36 System.out.print("号码:" + new String(ch, start, length));
37 }
38 if (tag.equals("ADDR")) {
39 System.out.println("地址:" + new String(ch, start, length));
40 }
41 }
42
43 public void startElement(String uri, String localName, String qName,
44 Attributes attrs) {
45 tags.push(qName);
46 }
47
48 @Override
49 public void endElement(String uri, String localName, String name)
50 throws SAXException {
51 tags.pop();
52 }
53
54}
55
第三种:jdom方式
1
package
com.test;
2
3 import java.io.File;
4 import java.util.List;
5
6 import org.jdom.Document;
7 import org.jdom.Element;
8 import org.jdom.input.SAXBuilder;
9
10 public class JdomParser {
11
12 public static void main(String arge[]) {
13 long lasting = System.currentTimeMillis();
14 try {
15 SAXBuilder builder = new SAXBuilder();
16 Document doc = builder.build(new File("src/test.xml"));
17 Element foo = doc.getRootElement();
18 List<Element> allChildren = foo.getChildren();
19 for (int i = 0; i < allChildren.size(); i++) {
20 System.out.print("号码:"
21 + allChildren.get(i).getChild("NO").getText());
22 System.out.println("地址:"
23 + allChildren.get(i).getChild("ADDR").getText());
24 }
25 } catch (Exception e) {
26 e.printStackTrace();
27 }
28 System.out.println("运行时间:" + (System.currentTimeMillis() - lasting)
29 + " 毫秒");
30 }
31}
32
特点:需要jdom.jar包支持,代码量少,简单。
2
3 import java.io.File;
4 import java.util.List;
5
6 import org.jdom.Document;
7 import org.jdom.Element;
8 import org.jdom.input.SAXBuilder;
9
10 public class JdomParser {
11
12 public static void main(String arge[]) {
13 long lasting = System.currentTimeMillis();
14 try {
15 SAXBuilder builder = new SAXBuilder();
16 Document doc = builder.build(new File("src/test.xml"));
17 Element foo = doc.getRootElement();
18 List<Element> allChildren = foo.getChildren();
19 for (int i = 0; i < allChildren.size(); i++) {
20 System.out.print("号码:"
21 + allChildren.get(i).getChild("NO").getText());
22 System.out.println("地址:"
23 + allChildren.get(i).getChild("ADDR").getText());
24 }
25 } catch (Exception e) {
26 e.printStackTrace();
27 }
28 System.out.println("运行时间:" + (System.currentTimeMillis() - lasting)
29 + " 毫秒");
30 }
31}
32
第四种:dom4j方式
1
package
com.test;
2
3 import java.io.File;
4 import java.util.Iterator;
5
6 import org.dom4j.Document;
7 import org.dom4j.Element;
8 import org.dom4j.io.SAXReader;
9
10 public class Dom4jParser {
11
12 public static void main(String arge[]) {
13 long lasting = System.currentTimeMillis();
14 try {
15 File f = new File("src/test.xml");
16 SAXReader reader = new SAXReader();
17 Document doc = reader.read(f);
18 Element root = doc.getRootElement();
19 Element foo;
20 for (Iterator i = root.elementIterator("VALUE"); i.hasNext();) {
21 foo = (Element) i.next();
22 System.out.print("号码:" + foo.elementText("NO"));
23 System.out.println("地址:" + foo.elementText("ADDR"));
24 }
25 } catch (Exception e) {
26 e.printStackTrace();
27 }
28 System.out.println("运行时间:" + (System.currentTimeMillis() - lasting)
29 + " 毫秒");
30 }
31}
32
特点:依赖dom4j.jar包,易用,支持集合处理。
2
3 import java.io.File;
4 import java.util.Iterator;
5
6 import org.dom4j.Document;
7 import org.dom4j.Element;
8 import org.dom4j.io.SAXReader;
9
10 public class Dom4jParser {
11
12 public static void main(String arge[]) {
13 long lasting = System.currentTimeMillis();
14 try {
15 File f = new File("src/test.xml");
16 SAXReader reader = new SAXReader();
17 Document doc = reader.read(f);
18 Element root = doc.getRootElement();
19 Element foo;
20 for (Iterator i = root.elementIterator("VALUE"); i.hasNext();) {
21 foo = (Element) i.next();
22 System.out.print("号码:" + foo.elementText("NO"));
23 System.out.println("地址:" + foo.elementText("ADDR"));
24 }
25 } catch (Exception e) {
26 e.printStackTrace();
27 }
28 System.out.println("运行时间:" + (System.currentTimeMillis() - lasting)
29 + " 毫秒");
30 }
31}
32
总结:
JDOM 与 DOM 主要有两方面不同。首先,JDOM 仅使用具体类而不使用接口。这在某些方面简化了 API,但是也限制了灵活性。第二,API 大量使用了 Collections 类,简化了那些已经熟悉这些类的 Java 开发者的使用。
JDOM 和 DOM 在性能测试时表现不佳。在小文档情况下还值得考虑使用 DOM 和 JDOM。另外,DOM 仍是一个非常好的选择。DOM 实现广泛应用于多种编程语言。它还是许多其它与 XML 相关的标准的基础,因为它正式获得 W3C 推荐(与基于非标准的 Java 模型相对),所以在某些类型的项目中可能也需要它(如在 JavaScript 中使用 DOM)。
SAX表现较好,这要依赖于它特定的解析方式。一个 SAX 检测即将到来的XML流,但并没有载入到内存(当然当XML流被读入时,会有部分文档暂时隐藏在内存中)。
无疑,DOM4J是这四种中最好的,目前许多开源项目中大量采用 DOM4J,例如大名鼎鼎的 Hibernate 也用 DOM4J 来读取 XML 配置文件。如果不考虑可移植性,那就采用DOM4J吧!