java使用xpath和dom4j解析xml
1 XML文件解析的4種方法
通常解析XML文件有四種經(jīng)典的方法?;镜慕馕龇绞接袃煞N,一種叫SAX,另一種叫DOM。SAX是基于事件流的解析,DOM是基于XML文檔樹結(jié)構(gòu)的解析。在此基礎(chǔ)上,為了減少DOM、SAX的編碼量,出現(xiàn)了JDOM,其優(yōu)點(diǎn)是,20-80原則(帕累托法則),極大減少了代碼量。通常情況下JDOM使用時(shí)滿足要實(shí)現(xiàn)的功能簡單,如解析、創(chuàng)建等要求。但在底層,JDOM還是使用SAX(最常用)、DOM、Xanan文檔。另外一種是DOM4J,是一個(gè)非常非常優(yōu)秀的Java XML API,具有性能優(yōu)異、功能強(qiáng)大和極端易用的特點(diǎn),同時(shí)它也是一個(gè)開放源代碼的軟件。如今你可以看到越來越多的 Java 軟件都在使用 DOM4J 來讀寫 XML,特別值得一提的是連 Sun 的 JAXM 也在用 DOM4J。具體四種方法的使用,百度一下,會有眾多詳細(xì)的介紹。
2 XPath簡單介紹
XPath是一門在XML文檔中查找信息的語言。XPath用于在 XML 文檔中通過元素和屬性進(jìn)行導(dǎo)航,并對元素和屬性進(jìn)行遍歷。XPath 是 W3C XSLT 標(biāo)準(zhǔn)的主要元素,并且 XQuery 和 XPointer 同時(shí)被構(gòu)建于 XPath 表達(dá)之上。因此,對 XPath 的理解是很多高級 XML 應(yīng)用的基礎(chǔ)。XPath非常類似對數(shù)據(jù)庫操作的SQL語言,或者說JQuery,它可以方便開發(fā)者抓起文檔中需要的東西。其中DOM4J也支持XPath的使用。
3 DOM4J使用XPath
DOM4J使用XPath解析XML文檔是,首先需要在項(xiàng)目中引用兩個(gè)JAR包:
dom4j-1.6.1.jar:DOM4J軟件包,下載地址http://sourceforge.net/projects/dom4j/;
jaxen-xx.xx.jar:通常不添加此包,會引發(fā)異常(java.lang.NoClassDefFoundError: org/jaxen/JaxenException),下載地址http://www.jaxen.org/releases.html。
3.1 命名空間(namespace)的干擾
在處理由excel文件或其他格式文件轉(zhuǎn)換的xml文件時(shí),通常會遇到通過XPath解析得不到結(jié)果的情況。這種情況通常是由于命名空間的存在導(dǎo)致的。以下述內(nèi)容的XML文件為例,通過XPath=" // Workbook/ Worksheet / Table / Row[1]/ Cell[1]/Data[1] "進(jìn)行簡單的檢索,通常是沒有結(jié)果出現(xiàn)的。這就是由于命名空間namespace(xmlns="urn:schemas-microsoft-com:office:spreadsheet")導(dǎo)致的。
<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()函數(shù)):使用XPath語法中自帶的local-name() 和 namespace-uri() 指定你要使用的節(jié)點(diǎn)名和命名空間。 XPath表達(dá)式書寫較為麻煩。
第二種方法(read2()函數(shù)):設(shè)置XPath的命名空間,利用setNamespaceURIs()函數(shù)。
第三種方法(read3()函數(shù)):設(shè)置DocumentFactory()的命名空間 ,使用的函數(shù)是setXPathNamespaceURIs()。二和三兩種方法的XPath表達(dá)式書寫相對簡單。
第四種方法(read4()函數(shù)):方法和第三種一樣,但是XPath表達(dá)式不同(程序具體體現(xiàn)),主要是為了檢驗(yàn)XPath表達(dá)式的不同,主要指完整程度,是否會對檢索效率產(chǎn)生影響。
(以上四種方法均通過DOM4J結(jié)合XPath對XML文件進(jìn)行解析)
第五種方法(read5()函數(shù)):使用DOM結(jié)合XPath對XML文件進(jìn)行解析,主要是為了檢驗(yàn)性能差異。
沒有什么能夠比代碼更能說明問題的了!果斷上代碼!
packageXPath;
importjava.io.IOException;
importjava.io.InputStream;
importjava.util.HashMap;
importjava.util.List;
importjava.util.Map;
importjavax.xml.parsers.DocumentBuilder;
importjavax.xml.parsers.DocumentBuilderFactory;
importjavax.xml.parsers.ParserConfigurationException;
importjavax.xml.xpath.XPathConstants;
importjavax.xml.xpath.XPathExpression;
importjavax.xml.xpath.XPathExpressionException;
importjavax.xml.xpath.XPathFactory;
importorg.dom4j.Document;
importorg.dom4j.DocumentException;
importorg.dom4j.Element;
importorg.dom4j.XPath;
importorg.dom4j.io.SAXReader;
importorg.w3c.dom.NodeList;
importorg.xml.sax.SAXException;
/**
*DOM4JDOMXMLXPath
*/
publicclassTestDom4jXpath{
publicstaticvoidmain(String[]args){
read1();
read2();
read3();
read4();//read3()方法一樣,但是XPath表達(dá)式不同
read5();
}
publicstaticvoidread1(){
/*
*uselocal-name()andnamespace-uri()inXPath
*/
try{
longstartTime=System.currentTimeMillis();
SAXReaderreader=newSAXReader();
InputStreamin=TestDom4jXpath.class.getClassLoader().getResourceAsStream("XPath\\XXX.xml");
Documentdoc=reader.read(in);
/*Stringxpath="http://*[local-name()='Workbook'andnamespace-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]";*/
Stringxpath="http://*[local-name()='Row'][4]/*[local-name()='Cell'][3]/*[local-name()='Data'][1]";
System.err.println("=====uselocal-name()andnamespace-uri()inXPath====");
System.err.println("XPath:"+xpath);
@SuppressWarnings("unchecked")
List<Element>list=doc.selectNodes(xpath);
for(Objecto:list){
Elemente=(Element)o;
Stringshow=e.getStringValue();
System.out.println("show="+show);
longendTime=System.currentTimeMillis();
System.out.println("程序運(yùn)行時(shí)間:"+(endTime-startTime)+"ms");
}
}catch(DocumentExceptione){
e.printStackTrace();
}
}
publicstaticvoidread2(){
/*
*setxpathnamespace(setNamespaceURIs)
*/
try{
longstartTime=System.currentTimeMillis();
Mapmap=newHashMap();
map.put("Workbook","urn:schemas-microsoft-com:office:spreadsheet");
SAXReaderreader=newSAXReader();
InputStreamin=TestDom4jXpath.class.getClassLoader().getResourceAsStream("XPath\\XXX.xml");
Documentdoc=reader.read(in);
Stringxpath="http://Workbook:Row[4]/Workbook:Cell[3]/Workbook:Data[1]";
System.err.println("=====usesetNamespaceURIs()tosetxpathnamespace====");
System.err.println("XPath:"+xpath);
XPathx=doc.createXPath(xpath);
x.setNamespaceURIs(map);
@SuppressWarnings("unchecked")
List<Element>list=x.selectNodes(doc);
for(Objecto:list){
Elemente=(Element)o;
Stringshow=e.getStringValue();
System.out.println("show="+show);
longendTime=System.currentTimeMillis();
System.out.println("程序運(yùn)行時(shí)間:"+(endTime-startTime)+"ms");
}
}catch(DocumentExceptione){
e.printStackTrace();
}
}
publicstaticvoidread3(){
/*
*setDocumentFactory()namespace(setXPathNamespaceURIs)
*/
try{
longstartTime=System.currentTimeMillis();
Mapmap=newHashMap();
map.put("Workbook","urn:schemas-microsoft-com:office:spreadsheet");
SAXReaderreader=newSAXReader();
InputStreamin=TestDom4jXpath.class.getClassLoader().getResourceAsStream("XPath\\XXX.xml");
reader.getDocumentFactory().setXPathNamespaceURIs(map);
Documentdoc=reader.read(in);
Stringxpath="http://Workbook:Row[4]/Workbook:Cell[3]/Workbook:Data[1]";
System.err.println("=====usesetXPathNamespaceURIs()tosetDocumentFactory()namespace====");
System.err.println("XPath:"+xpath);
@SuppressWarnings("unchecked")
List<Element>list=doc.selectNodes(xpath);
for(Objecto:list){
Elemente=(Element)o;
Stringshow=e.getStringValue();
System.out.println("show="+show);
longendTime=System.currentTimeMillis();
System.out.println("程序運(yùn)行時(shí)間:"+(endTime-startTime)+"ms");
}
}catch(DocumentExceptione){
e.printStackTrace();
}
}
publicstaticvoidread4(){
/*
*同read3()方法一樣,但是XPath表達(dá)式不同
*/
try{
longstartTime=System.currentTimeMillis();
Mapmap=newHashMap();
map.put("Workbook","urn:schemas-microsoft-com:office:spreadsheet");
SAXReaderreader=newSAXReader();
InputStreamin=TestDom4jXpath.class.getClassLoader().getResourceAsStream("XPath\\XXX.xml");
reader.getDocumentFactory().setXPathNamespaceURIs(map);
Documentdoc=reader.read(in);
Stringxpath="http://Workbook:Worksheet/Workbook:Table/Workbook:Row[4]/Workbook:Cell[3]/Workbook:Data[1]";
System.err.println("=====usesetXPathNamespaceURIs()tosetDocumentFactory()namespace====");
System.err.println("XPath:"+xpath);
@SuppressWarnings("unchecked")
List<Element>list=doc.selectNodes(xpath);
for(Objecto:list){
Elemente=(Element)o;
Stringshow=e.getStringValue();
System.out.println("show="+show);
longendTime=System.currentTimeMillis();
System.out.println("程序運(yùn)行時(shí)間:"+(endTime-startTime)+"ms");
}
}catch(DocumentExceptione){
e.printStackTrace();
}
}
publicstaticvoidread5(){
/*
*DOMandXPath
*/
try{
longstartTime=System.currentTimeMillis();
DocumentBuilderFactorydbf=DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(false);
DocumentBuilderbuilder=dbf.newDocumentBuilder();
InputStreamin=TestDom4jXpath.class.getClassLoader().getResourceAsStream("XPath\\XXX.xml");
org.w3c.dom.Documentdoc=builder.parse(in);
XPathFactoryfactory=XPathFactory.newInstance();
javax.xml.xpath.XPathx=factory.newXPath();
//選取所有class元素的name屬性
Stringxpath="http://Workbook/Worksheet/Table/Row[4]/Cell[3]/Data[1]";
System.err.println("=====DomXPath====");
System.err.println("XPath:"+xpath);
XPathExpressionexpr=x.compile(xpath);
NodeListnodes=(NodeList)expr.evaluate(doc,XPathConstants.NODE);
for(inti=0;i<nodes.getLength();i++){
System.out.println("show="+nodes.item(i).getNodeValue());
longendTime=System.currentTimeMillis();
System.out.println("程序運(yùn)行時(shí)間:"+(endTime-startTime)+"ms");
}
}catch(XPathExpressionExceptione){
e.printStackTrace();
}catch(ParserConfigurationExceptione){
e.printStackTrace();
}catch(SAXExceptione){
e.printStackTrace();
}catch(IOExceptione){
e.printStackTrace();
}
}
}
PS:這里再為大家提供幾款關(guān)于xml操作的在線工具供大家參考使用:
在線XML/JSON互相轉(zhuǎn)換工具:
http://tools.jb51.net/code/xmljson
在線格式化XML/在線壓縮XML:
http://tools.jb51.net/code/xmlformat
XML在線壓縮/格式化工具:
http://tools.jb51.net/code/xml_format_compress
相關(guān)文章
spring定時(shí)任務(wù)執(zhí)行兩次及tomcat部署緩慢問題的解決方法
這篇文章主要給大家介紹了關(guān)于spring定時(shí)任務(wù)執(zhí)行兩次及tomcat部署緩慢問題的解決方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面小編來一起學(xué)習(xí)學(xué)習(xí)吧。2018-01-01解決MyBatis中模糊搜索使用like匹配帶%字符時(shí)失效問題
Mybatis是我們?nèi)粘m?xiàng)目中經(jīng)常使用的框架,在項(xiàng)目中我們一般會使用like查詢作為模糊匹配字符進(jìn)行搜索匹配,下面的Mapper.xml是我們使用like在項(xiàng)目中進(jìn)行模糊匹配的常用方式,感興趣的朋友跟隨小編一起看看吧2021-09-09詳解lombok @Getter @Setter 使用注意事項(xiàng)
這篇文章主要介紹了詳解lombok @Getter @Setter 使用注意事項(xiàng),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11java Gui實(shí)現(xiàn)肯德基點(diǎn)餐收銀系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了java Gui實(shí)現(xiàn)肯德基點(diǎn)餐收銀系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-01-01Java 入門圖形用戶界面設(shè)計(jì)之列表框JList
圖形界面(簡稱GUI)是指采用圖形方式顯示的計(jì)算機(jī)操作用戶界面。與早期計(jì)算機(jī)使用的命令行界面相比,圖形界面對于用戶來說在視覺上更易于接受,本篇精講Java語言中關(guān)于圖形用戶界面的列表框JList2022-02-02使用Java計(jì)算集合中的組內(nèi)平均值的代碼實(shí)現(xiàn)
在Java開發(fā)中,集合(Collection)是一個(gè)重要的數(shù)據(jù)結(jié)構(gòu),廣泛應(yīng)用于各種場景,計(jì)算集合中的組內(nèi)平均值是一個(gè)常見的操作,本文將深入探討如何使用Java來計(jì)算集合中的組內(nèi)平均值,涵蓋基本概念、具體實(shí)現(xiàn)、優(yōu)化策略和實(shí)用示例,需要的朋友可以參考下2024-06-06java 如何往已經(jīng)存在的excel表格里面追加數(shù)據(jù)的方法
這篇文章主要介紹了java 如何往已經(jīng)存在的excel表格里面追加數(shù)據(jù)的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08詳解IntelliJ IDEA 自帶的 HTTP Client 接口調(diào)用插件吊打 Postman
HTTP Client 是 IDEA 自帶的一款簡潔輕量級的接口調(diào)用插件,通過它,我們能在 IDEA 上開發(fā),調(diào)試,測試 RESTful Web 服務(wù),接下來通過本文給大家分享IntelliJ IDEA 自帶的 HTTP Client 接口調(diào)用插件吊打 Postman的知識,感興趣的朋友一起看看吧2021-05-05