解析XML的4種方式及優缺點比較


dom, sax是解析xml的底層接口
而jdom和dom4j則是基於底層api的更高級封裝   
dom是通用的,而jdom和dom4j則是面向java語言的

 

(方法一). DOM解析說明:為 XML 文檔的已解析版本定義了一組接口。解析器讀入整個文檔,然后構建一個駐留內存的樹結
構, 然后代碼就可以使用 DOM 接口來操作這個樹結構。優點:整個文檔樹在內存中,便於操作;支持刪除、修改、重新排列等多種功能;缺點:將整個文檔調入內 存(包括無用的節點),浪費時間和空間;使用場合:一旦解析了文檔還需多次訪問這些數據;硬件資源充足(內存、CPU).

 1 import java.io.File;
 2 
 3 import javax.xml.parsers.DocumentBuilder;
 4 import javax.xml.parsers.DocumentBuilderFactory;
 5 
 6 import org.w3c.dom.Attr;
 7 import org.w3c.dom.Document;
 8 import org.w3c.dom.Element;
 9 import org.w3c.dom.NamedNodeMap;
10 import org.w3c.dom.Node;
11 import org.w3c.dom.NodeList;
12 
13 public class DomDemo {
14 
15     private final static String xmlPath = "D:\\WHS01.xml";
16 
17     /**
18      * 遞歸輸出節點
19      * 
20      * @param element
21      */
22     public static void parseElement(Element element) {
23 
24         System.out.print("<" + element.getTagName());
25         NamedNodeMap map = element.getAttributes();
26         if (null != map) {
27             for (int i = 0; i < map.getLength(); i++) {
28                 Attr attr = (Attr) map.item(i);
29                 System.out.print(" " + attr.getName() + "=\"" + attr.getValue()
30                         + "\"");
31             }
32         }
33         System.out.print(">");
34 
35         NodeList childList = element.getChildNodes();
36 
37         for (int i = 0; i < childList.getLength(); i++) {
38             Node node = childList.item(i);
39 
40             switch (node.getNodeType()) {
41             case Node.ELEMENT_NODE:
42                 parseElement((Element) node);
43                 break;
44             case Node.TEXT_NODE:
45                 System.out.print(node.getNodeValue());
46                 break;
47             default:
48                 break;
49             }
50         }
51         System.out.print("</" + element.getTagName() + ">");
52     }
53 
54     /**
55      * 根據指定路徑獲取Document對象
56      * 
57      * @param xmlPath
58      * @return
59      * @throws Exception
60      */
61     public static Document getDocument(String xmlPath) throws Exception {
62 
63         Document document = null;
64         if (null == xmlPath || "".equals(xmlPath.trim()))
65             return document;
66 
67         File file = new File(xmlPath);
68         if (file.canRead() && file.exists()) {
69             DocumentBuilderFactory factory = DocumentBuilderFactory
70                     .newInstance();
71             DocumentBuilder builder = factory.newDocumentBuilder();
72             document = builder.parse(file);
73         }
74         return document;
75     }
76 
77     public static void main(String[] args) {
78         Document document = null;
79         try {
80             document = getDocument(xmlPath);
81         } catch (Exception e) {
82             e.printStackTrace();
83         }
84         // 傳入根節點
85         parseElement(document.getDocumentElement());
86     }
87 }
View Code

(方法二)SAX解析說明:為解決DOM的問題,出現了SAX。SAX ,事件驅動。當解析器發現元素開始、元素結束、文本、文檔的

開 始或結束等時,發送事件,程序員編寫響應這些事件的代碼,保存數據。優點:不用事先調入整個文檔,占用資源少;SAX解析器代碼比DOM解析器代碼小,適 於Applet,下載。缺點:不是持久的;事件過后,若沒保存數據,那么數據就丟了;無狀態性;從事件中只能得到文本,但不知該文本屬於哪個元素;使用場 合:Applet;只需XML文檔的少量內容,很少回頭訪問;機器內存少

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <BOM Code="LM4029">
 3     <Child Code="LM4029MC">
 4         <Quantity>2.000000</Quantity>
 5     </Child>
 6     <Child Code="LM4029D">
 7         <Quantity>1.000000</Quantity>
 8     </Child>
 9     <Child Code="LM4029PH">
10         <Quantity>1.000000</Quantity>
11     </Child>
12     <Child Code ="LM4029PS">
13         <Quantity>1.000000</Quantity>
14     </Child>
15     <Child Code="LM4029SB">
16         <Quantity>1.000000</Quantity>
17     </Child>
18 </BOM>
XML
 1 import java.util.Stack;
 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.SAXException;
 8 import org.xml.sax.helpers.DefaultHandler;
 9 
10 public class SAXDemo {
11 
12     private final static String uri = "D:\\bom.xml";
13 
14     public static void main(String[] args) {
15         SAXParserFactory factory = SAXParserFactory.newInstance();
16         try {
17             SAXParser parser = factory.newSAXParser();
18             parser.parse(uri, new MyHandler());
19         } catch (Exception e) {
20             e.printStackTrace();
21         }
22     }
23 }
24 
25 class Child {
26     private String code;
27     private double quantity;
28 
29     public String getCode() {
30         return code;
31     }
32 
33     public void setCode(String code) {
34         this.code = code;
35     }
36 
37     public double getQuantity() {
38         return quantity;
39     }
40 
41     public void setQuantity(double quantity) {
42         this.quantity = quantity;
43     }
44 
45 }
46 
47 class MyHandler extends DefaultHandler {
48 
49     Stack<String> stack = null;
50     Child child = null;
51 
52     @Override
53     public void startDocument() throws SAXException {
54         System.out.println("start document");
55         stack = new Stack<String>();
56     }
57 
58     @Override
59     public void endDocument() throws SAXException {
60         System.out.println("end document");
61         stack = null;
62     }
63 
64     @Override
65     public void startElement(String uri, String localName, String qName,
66             Attributes attributes) throws SAXException {
67         stack.push(qName);
68         if ("Child".equals(qName)) {
69             child = new Child();
70             child.setCode(attributes.getValue("Code"));
71         }
72     }
73 
74     @Override
75     public void endElement(String uri, String localName, String qName)
76             throws SAXException {
77         stack.pop();
78         if ("Child".equals(qName)) {
79             System.out.println("Code -> " + child.getCode() + ", Quantity -> "
80                     + child.getQuantity());
81             child = null;
82         }
83     }
84 
85     @Override
86     public void characters(char[] ch, int start, int length)
87             throws SAXException {
88         if ("Quantity".equals(stack.peek())) {
89             child.setQuantity(Double.parseDouble(new String(ch, start, length)));
90         }
91     }
92 }
View Code

(方法三)JDOM解析說明:為減少DOM、SAX的編碼量,出現了JDOM;優點:20-80原則,極大減少了代碼量。使用場合:要實現的功能
簡單,如解析、創建等,但在底層,JDOM還是使用SAX(最常用),DOM

 1 import java.io.File;
 2 import java.io.IOException;
 3 import java.util.List;
 4 
 5 import org.jdom2.Document;
 6 import org.jdom2.Element;
 7 import org.jdom2.JDOMException;
 8 import org.jdom2.input.SAXBuilder;
 9 
10 public class JdomDemo {
11 
12     private final static String xmlPath = "D:\\bom.xml";
13 
14     public static void readXml() throws JDOMException, IOException {
15         SAXBuilder builder = new SAXBuilder();
16         Document document = builder.build(new File(xmlPath));
17 
18         Element element = document.getRootElement();
19         List<Element> childList = element.getChildren("Child");
20 
21         for (int i = 0; i < childList.size(); i++) {
22             String code = childList.get(i).getAttributeValue("Code");
23             double quantity = Double.parseDouble(childList.get(i).getChildText(
24                     "Quantity"));
25             System.out.println("Code -> " + code + ", Quantity -> " + quantity);
26         }
27     }
28 }
View Code

(方法四)DOM4J解析說明:DOM4J 是一個非常非常優秀的Java XML API,具有性能優異、功能強大和極端易用使用的特點,同

時它也是一個開放源代碼的軟件。如今你可以看到越來越多的 Java 軟件都在使用 DOM4J 來讀寫 XML

 1 import java.io.File;
 2 import java.io.IOException;
 3 import java.util.Iterator;
 4 
 5 import javax.xml.parsers.DocumentBuilder;
 6 import javax.xml.parsers.DocumentBuilderFactory;
 7 import javax.xml.parsers.ParserConfigurationException;
 8 
 9 import org.dom4j.Attribute;
10 import org.dom4j.Document;
11 import org.dom4j.DocumentException;
12 import org.dom4j.Element;
13 import org.dom4j.io.DOMReader;
14 import org.dom4j.io.SAXReader;
15 import org.xml.sax.SAXException;
16 
17 public class Dom4jDemo {
18 
19     private final static String xmlPath = "D:\\bom.xml";
20 
21     public static void parseElement() throws DocumentException,
22             ParserConfigurationException, SAXException, IOException {
23         // 方式一 DOM 
24         // DocumentBuilderFactory factory =
25         // DocumentBuilderFactory.newInstance();
26         // DocumentBuilder builder = factory.newDocumentBuilder();
27         // org.w3c.dom.Document domDoc = builder.parse(xmlPath);
28         // DOMReader domReader = new DOMReader();
29         // Document document = domReader.read(domDoc);
30 
31         //方式二 SAX(常用)
32         SAXReader saxReader = new SAXReader();
33         Document document = saxReader.read(new File(xmlPath));
34         Element rootEl = document.getRootElement();
35 
36         for (Iterator iterator = rootEl.elementIterator("Child"); iterator
37             .hasNext();) {
38             Element e = (Element) iterator.next();
39             System.out.print("Code -> " + e.attributeValue("Code"));
40             System.out.println(" Quantity -> " + e.elementText("Quantity"));
41         }
42     }
43 
44 }
View Code

總結:

1.DOM, JDOM在性能上不如SAX, DOM4J, 在小文檔情況下還值得考慮使用 DOM 和 JDOM。

2.DOM 實現廣泛應用於多種編程語言。它還是許多其它與 XML 相關的標准的基礎,因為它正式獲得 W3C 推薦(與基於非標准的 Java 模型相對),所以在某些類型的項目中可能也需要它(如在 javascript 中使用 DOM)。

3.SAX 的高效取決於特定的解析方式,不用事先調入整個文檔,占用資源少。

4.如果不考慮可移植性, 首先考慮使用DOM4J。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM