码迷,mamicode.com
首页 > 其他好文 > 详细

XML的两种解析方式,Dom4j and SAX parse

时间:2015-07-09 14:28:59      阅读:97      评论:0      收藏:0      [点我收藏+]

标签:

Dom4j and Sax difference。

  • Dom4j 解析的速度慢,而且消耗内存,因为在解析之前要先把文件放到内存中。并采用基于对象的模型解析

  有以下几点

  1. Dom4J parse loads the entire XML file into memory before parsing.
  2. It uses Object based Model for parsing XML.
  3. High memory usage - as it loads the file to memory.
  4. We can insert or delete nodes.
  5. Traverse in any direction.
  6. With Dom4J we can use all the XPATH expressions.
   代码如下:
 1        SAXReader reader = new SAXReader();
 2             InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream("data.xml");
 3 //            InputStream is = new FileInputStream(new File("C:/test/data.xml"));
 4             Document doc = reader.read(is);
 5             Element ele = doc.getRootElement();
 6             @SuppressWarnings("unchecked")
 7             List<Element> list = ele.elements();
 8             for(Element elelist : list){
 9                 if("driver".equals(elelist.getName())){
10                     driver = elelist.getTextTrim();
11                 }
12                 if("url".equals(elelist.getName())){
13                     url = elelist.getTextTrim();
14                 }
15                 if("password".equals(elelist.getName())){
16                     password = elelist.getTextTrim();
17                 }
18                 if("username".equals(elelist.getName())){
19                     username = elelist.getTextTrim();
20                 }
21                     
22             }

 

  • SAX 解析是快速的,内存的消耗小,并采用事件模型解析。

  有以下几点

  1. SAX parses the XML file Node by Node.
  2. It uses Event based Model for parsing XML.
  3. Low memory usage as it does not loads the entire file to memory instead read it node by node.
  4. We cant insert or delete a node.
  5. Top to bottom traversing.
  6. With SAX parser we can only use basic XPATH expressions and can not use expressions like last etc.
 
  对于大文件xml 的读取可以使用SAX,如果想要使用复杂的xpath 表达式可以使用dom4j.
 
   使用SAX 解析的时候我们要继承 DefaultHandler , 在这有startDocument(),endDocument(),startElement(),endElement(),characters() etc. 方法。
  当我们解析的时候会逐行解析,放到 characters() 方法中,然后你可以在endElement()方法中取出各个元素的值。
  代码如下:
 1 package com.devil.util;
 2 
 3 import java.io.IOException;
 4 
 5 import javax.xml.parsers.ParserConfigurationException;
 6 import javax.xml.parsers.SAXParser;
 7 import javax.xml.parsers.SAXParserFactory;
 8 
 9 import org.xml.sax.Attributes;
10 import org.xml.sax.SAXException;
11 import org.xml.sax.helpers.DefaultHandler;
12 
13 public class SAXParse extends DefaultHandler {
14     private static String driver = null;
15     private static String url = null;
16     private static String username = null;
17     private static String password = null;
18     private String elementValue = null;
19 
20     static {
21         SAXParserFactory factory = SAXParserFactory.newInstance();
22         try {
23             SAXParser parse = factory.newSAXParser();
24             parse.parse(Thread.currentThread().getContextClassLoader().getResourceAsStream("data.xml"), new SAXParse());
25         } catch (ParserConfigurationException e) {
26             e.printStackTrace();
27         } catch (SAXException e) {
28             e.printStackTrace();
29         } catch (IOException e) {
30             e.printStackTrace();
31         }
32         
33     }
34     @Override
35     public void startElement(String paramString1, String localName,
36             String qname, Attributes pattr) throws SAXException {
37     }
38 
39     @Override
40     public void characters(char[] ch, int start, int end) throws SAXException {
41         elementValue = new String(ch, start, end);
42 
43     }
44 
45     @Override
46     public void endElement(String NamespaceUrl, String localName, String qname)
47             throws SAXException {
48 
49         if (qname.equals("driver")) {
50             driver = elementValue.trim();
51         }
52         if ("url".equals(qname)) {
53             url = elementValue.trim();
54         }
55         if ("password".equals(qname)) {
56             password = elementValue.trim();
57         }
58         if ("username".equals(qname)) {
59             username = elementValue.trim();
60         }
61 
62     }
63 
64     public static String getDriver() {
65         return driver;
66     }
67 
68     public static String getUrl() {
69         return url;
70     }
71 
72     public static String getUsername() {
73         return username;
74     }
75 
76     public static String getPassword() {
77         return password;
78     }
79 
80     
81     public static void main(String[] args)  {
82         
83         System.out.println(SAXParse.getDriver());
84     }
85 }

 

XML的两种解析方式,Dom4j and SAX parse

标签:

原文地址:http://www.cnblogs.com/Devilwx/p/4632199.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!