最近在做一個小項目,使用到XML文件解析技術,通過對該技術的了解和使用,總結了以下內容。
1 XML文件解析的4種方法
通常解析XML文件有四種經典的方法。基本的解析方式有兩種,一種叫SAX,另一種叫DOM。SAX是基于事件流的解析,DOM是基于XML文檔樹結構的解析。在此基礎上,為了減少DOM、SAX的編碼量,出現了JDOM,其優點是,20-80原則(帕累托法則),極大減少了代碼量。通常情況下JDOM使用時滿足要實現的功能簡單,如解析、創建等要求。但在底層,JDOM還是使用SAX(最常用)、DOM、Xanan文檔。另外一種是DOM4J,是一個非常非常優秀的Java XML API,具有性能優異、功能強大和極端易用的特點,同時它也是一個開放源代碼的軟件。如今你可以看到越來越多的 Java 軟件都在使用 DOM4J 來讀寫 XML,特別值得一提的是連 Sun 的 JAXM 也在用 DOM4J。
2 XPath簡單介紹
XPath 是一門在 XML 文檔中查找信息的語言。XPath 用于在 XML 文檔中通過元素和屬性進行導航,并對元素和屬性進行遍歷。XPath 是 W3C XSLT 標準的主要元素,并且 XQuery 和 XPointer 同時被構建于 XPath 表達之上。因此,對 XPath 的理解是很多高級 XML 應用的基礎。XPath非常類似對數據庫操作的SQL語言,或者說JQuery,它可以方便開發者抓起文檔中需要的東西。其中DOM4J也支持XPath的使用。
3 DOM4J使用XPath
DOM4J使用XPath解析XML文檔是,首先需要在項目中引用兩個JAR包:
dom4j-1.6.1.jar:DOM4J軟件包,下載地址http://sourceforge.net/projects/dom4j/;
jaxen-xx.xx.jar:通常不添加此包,會引發異常(java.lang.NoClassDefFoundError: org/jaxen/JaxenException),下載地址http://www.jaxen.org/releases.html。
3.1 命名空間(namespace)的干擾
在處理由excel文件或其他格式文件轉換的xml文件時,通常會遇到通過XPath解析得不到結果的情況。這種情況通常是由于命名空間的存在導致的。以下述內容的XML文件為例,通過XPath=" // Workbook/ Worksheet / Table / Row[1]/ Cell[1]/Data[1] "進行簡單的檢索,通常是沒有結果出現的。這就是由于命名空間namespace(xmlns="urn:schemas-microsoft-com:office:spreadsheet")導致的。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
< Workbook xmlns = "urn:schemas-microsoft-com:office:spreadsheet" xmlns:o = "urn:schemas-microsoft-com:office:office" xmlns:x = "urn:schemas-microsoft-com:office:excel" xmlns:ss = "urn:schemas-microsoft-com:office:spreadsheet" xmlns:html = "http://www.w3.org/TR/REC-html40" > < Worksheet ss:Name = "Sheet1" > < Table ss:ExpandedColumnCount = "81" ss:ExpandedRowCount = "687" x:FullColumns = "1" x:FullRows = "1" ss:DefaultColumnWidth = "52.5" ss:DefaultRowHeight = "15.5625" > < Row ss:AutoFitHeight = "0" > < Cell > < Data ss:Type = "String" >敲代碼的耗子</ Data > </ Cell > </ Row > < Row ss:AutoFitHeight = "0" > < Cell > < Data ss:Type = "String" >Sunny</ Data > </ Cell > </ Row > </ Table > </ Worksheet > </ Workbook > |
3.2 XPath對帶有命名空間的xml文件解析
第一種方法(read1()函數):使用XPath語法中自帶的local-name() 和 namespace-uri() 指定你要使用的節點名和命名空間。 XPath表達式書寫較為麻煩。
第二種方法(read2()函數):設置XPath的命名空間,利用setNamespaceURIs()函數。
第三種方法(read3()函數):設置DocumentFactory()的命名空間 ,使用的函數是setXPathNamespaceURIs()。二和三兩種方法的XPath表達式書寫相對簡單。
第四種方法(read4()函數):方法和第三種一樣,但是XPath表達式不同(程序具體體現),主要是為了檢驗XPath表達式的不同,主要指完整程度,是否會對檢索效率產生影響。(以上四種方法均通過DOM4J結合XPath對XML文件進行解析)
第五種方法(read5()函數):使用DOM結合XPath對XML文件進行解析,主要是為了檢驗性能差異。
沒有什么能夠比代碼更能說明問題的了!果斷上代碼!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
|
package XPath; import java.io.IOException; import java.io.InputStream; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import javax.xml.xpath.XPathConstants; import javax.xml.xpath.XPathExpression; import javax.xml.xpath.XPathExpressionException; import javax.xml.xpath.XPathFactory; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.Element; import org.dom4j.XPath; import org.dom4j.io.SAXReader; import org.w3c.dom.NodeList; import org.xml.sax.SAXException; /** * DOM4J DOM XML XPath * @author hao */ public class TestDom4jXpath { public static void main(String[] args) { read1(); read2(); read3(); read4(); //read3()方法一樣,但是XPath表達式不同 read5(); } public static void read1() { /* * use local-name() and namespace-uri() in XPath */ try { long startTime=System.currentTimeMillis(); SAXReader reader = new SAXReader(); InputStream in = TestDom4jXpath.class.getClassLoader().getResourceAsStream("XPath\\XXX.xml"); Document doc = reader.read(in); /*String xpath ="//*[local-name()='Workbook' and namespace-uri()='urn:schemas-microsoft-com:office:spreadsheet']" + "/*[local-name()='Worksheet']" + "/*[local-name()='Table']" + "/*[local-name()='Row'][4]" + "/*[local-name()='Cell'][3]" + "/*[local-name()='Data'][1]";*/ String xpath ="//*[local-name()='Row'][4]/*[local-name()='Cell'][3]/*[local-name()='Data'][1]"; System.err.println("=====use local-name() and namespace-uri() in XPath===="); System.err.println("XPath:" + xpath); @SuppressWarnings("unchecked") List<Element> list = doc.selectNodes(xpath); for(Object o:list){ Element e = (Element) o; String show=e.getStringValue(); System.out.println("show = " + show); long endTime=System.currentTimeMillis(); System.out.println("程序運行時間: "+(endTime-startTime)+"ms"); } } catch (DocumentException e) { e.printStackTrace(); } } public static void read2() { /* * set xpath namespace(setNamespaceURIs) */ try { long startTime=System.currentTimeMillis(); Map map = new HashMap(); map.put("Workbook","urn:schemas-microsoft-com:office:spreadsheet"); SAXReader reader = new SAXReader(); InputStream in = TestDom4jXpath.class.getClassLoader().getResourceAsStream("XPath\\XXX.xml"); Document doc = reader.read(in); String xpath ="//Workbook:Row[4]/Workbook:Cell[3]/Workbook:Data[1]"; System.err.println("=====use setNamespaceURIs() to set xpath namespace===="); System.err.println("XPath:" + xpath); XPath x = doc.createXPath(xpath); x.setNamespaceURIs(map); @SuppressWarnings("unchecked") List<Element> list = x.selectNodes(doc); for(Object o:list){ Element e = (Element) o; String show=e.getStringValue(); System.out.println("show = " + show); long endTime=System.currentTimeMillis(); System.out.println("程序運行時間: "+(endTime-startTime)+"ms"); } } catch (DocumentException e) { e.printStackTrace(); } } public static void read3() { /* * set DocumentFactory() namespace(setXPathNamespaceURIs) */ try { long startTime=System.currentTimeMillis(); Map map = new HashMap(); map.put("Workbook","urn:schemas-microsoft-com:office:spreadsheet"); SAXReader reader = new SAXReader(); InputStream in = TestDom4jXpath.class.getClassLoader().getResourceAsStream("XPath\\XXX.xml"); reader.getDocumentFactory().setXPathNamespaceURIs(map); Document doc = reader.read(in); String xpath ="//Workbook:Row[4]/Workbook:Cell[3]/Workbook:Data[1]"; System.err.println("=====use setXPathNamespaceURIs() to set DocumentFactory() namespace===="); System.err.println("XPath:" + xpath); @SuppressWarnings("unchecked") List<Element> list = doc.selectNodes(xpath); for(Object o:list){ Element e = (Element) o; String show=e.getStringValue(); System.out.println("show = " + show); long endTime=System.currentTimeMillis(); System.out.println("程序運行時間: "+(endTime-startTime)+"ms"); } } catch (DocumentException e) { e.printStackTrace(); } } public static void read4() { /* * 同read3()方法一樣,但是XPath表達式不同 */ try { long startTime=System.currentTimeMillis(); Map map = new HashMap(); map.put("Workbook","urn:schemas-microsoft-com:office:spreadsheet"); SAXReader reader = new SAXReader(); InputStream in = TestDom4jXpath.class.getClassLoader().getResourceAsStream("XPath\\XXX.xml"); reader.getDocumentFactory().setXPathNamespaceURIs(map); Document doc = reader.read(in); String xpath ="//Workbook:Worksheet/Workbook:Table/Workbook:Row[4]/Workbook:Cell[3]/Workbook:Data[1]"; System.err.println("=====use setXPathNamespaceURIs() to set DocumentFactory() namespace===="); System.err.println("XPath:" + xpath); @SuppressWarnings("unchecked") List<Element> list = doc.selectNodes(xpath); for(Object o:list){ Element e = (Element) o; String show=e.getStringValue(); System.out.println("show = " + show); long endTime=System.currentTimeMillis(); System.out.println("程序運行時間: "+(endTime-startTime)+"ms"); } } catch (DocumentException e) { e.printStackTrace(); } } public static void read5() { /* * DOM and XPath */ try { long startTime=System.currentTimeMillis(); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setNamespaceAware( false ); DocumentBuilder builder = dbf.newDocumentBuilder(); InputStream in = TestDom4jXpath. class .getClassLoader().getResourceAsStream( "XPath\\XXX.xml" ); org.w3c.dom.Document doc = builder.parse(in); XPathFactory factory = XPathFactory.newInstance(); javax.xml.xpath.XPath x = factory.newXPath(); //選取所有class元素的name屬性 String xpath = "//Workbook/Worksheet/Table/Row[4]/Cell[3]/Data[1]" ; System.err.println( "=====Dom XPath====" ); System.err.println( "XPath:" + xpath); XPathExpression expr = x.compile(xpath); NodeList nodes = (NodeList)expr.evaluate(doc, XPathConstants.NODE); for ( int i = 0 ; i<nodes.getLength();i++) { System.out.println( "show = " + nodes.item(i).getNodeValue()); long endTime=System.currentTimeMillis(); System.out.println( "程序運行時間: " +(endTime-startTime)+ "ms" ); } } catch (XPathExpressionException e) { e.printStackTrace(); } catch (ParserConfigurationException e) { e.printStackTrace(); } catch (SAXException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } } |
3.3 不同方法的性能比較
為了比較幾種方法的解析性能,實驗過程中使用了6M以上大小,7萬行以上的XML文件(XXX.xml)進行10輪測試,如下所述:
圖1 XPath使用性能對比
方法名稱 |
平均運行時間 |
XPath表達式 |
read1() |
1663ms |
//*[local-name()='Row'][4]/*[local-name()='Cell'][3]/*[local-name()='Data'][1] |
read2() |
2184ms |
//Workbook:Row[4]/Workbook:Cell[3]/Workbook:Data[1] |
read3() |
601ms |
//Workbook:Row[4]/Workbook:Cell[3]/Workbook:Data[1] |
read4() |
472ms |
//Workbook:Worksheet/Workbook:Table/Workbook:Row[4]/Workbook:Cell[3]/Workbook:Data[1] |
read5() |
1094ms |
//Workbook/Worksheet/Table/Row[4]/Cell[3]/Data[1] |
表1 平均性能統計
由以上性能對比可知:
1、read4()方法運行時間最短,即運用DOM4J方法調用全路徑(從根節點出發)XPath表達式解析XML文件耗時最短;
2、運用DOM解析方法所使用的XPath表達式最為簡單(可以寫作//Row[4]/Cell[3]/Data[1]),因DOM中可以通過setNamespaceAware(false)方法使命名空間失效。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:http://www.cnblogs.com/wangzhongqiu/p/6402940.html