XPath詳解及Java示例代碼

  1. import java.io.IOException;  
  2.   
  3. import javax.xml.parsers.*;  
  4. import javax.xml.xpath.*;  
  5. import org.w3c.dom.*;  
  6. import org.xml.sax.SAXException;  
  7.   
  8. public class XpathTest {  
  9.   
  10.     public static void main(String[] args) throws ParserConfigurationException,  
  11.             SAXException, IOException, XPathExpressionException {  
  12.         DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();  
  13.         factory.setNamespaceAware(false);  
  14.         DocumentBuilder builder = factory.newDocumentBuilder();  
  15.         Document doc = builder.parse("C:/Users/Administrator/Desktop/test.xml");  
  16.         System.out.println(doc.getChildNodes().getLength());  
  17.         XPathFactory xFactory = XPathFactory.newInstance();  
  18.         XPath xpath = xFactory.newXPath();  
  19.         XPathExpression expr = xpath  
  20.                 .compile("//name/text()");  
  21.         Object result = expr.evaluate(doc, XPathConstants.NODESET);  
  22.         NodeList nodes = (NodeList) result;  
  23.         System.out.println(nodes.getLength());  
  24.         for (int i = 0; i < nodes.getLength(); i++) {  
  25.             System.out.println(nodes.item(i).getNodeValue());  
  26.         }  
  27.     }  
  28.   
  29. }  


一、結點類型
XPath中有七種結點類型:元素、屬性、文本、命名空間、處理指令、註釋以及文檔節點(或成爲根節點)。 文檔的根節點即是文檔結點;對應屬性有屬性結點,元素有元素結點。
 
二、常用路徑表達式
表達式 描述
nodename                                      選取此節點的所有子節點
/                                      從根節點選取
//                                      從匹配選擇的當前節點選擇文檔中的節點,而不考慮它們的位置
.                                      選取當前節點
..                                      選取當前節點的父節點
@                                      選取屬性
  例如有文檔:
[xhtml] view plaincopy
 
 
  1. <?xml version="1.0" encoding="ISO-8859-1"?>  
  2. <bookstore>  
  3. <book>  
  4.   <title lang="eng">Harry Potter</title>  
  5.   <price>29.99</price>  
  6. </book>  
  7. <book>  
  8.   <title lang="eng">Learning XML</title>  
  9.   <price>39.95</price>  
  10. </book>  
  11. </bookstore>  
則:
路徑表達式 結果
bookstore 選取 bookstore 元素的所有子節點
/bookstore 選取根元素 bookstore 註釋:假如路徑起始於正斜槓( / ),則此路徑始終代表到某元素的絕對路徑!
bookstore/book 選取所有屬於 bookstore 的子元素的 book 元素。
//book 選取所有 book 子元素,而不管它們在文檔中的位置。
bookstore//book 選擇所有屬於 bookstore 元素的後代的 book 元素,而不管它們位於 bookstore 之下的什麼位置。
//@lang 選取所有名爲 lang 的屬性。
 
三、限定語
用來查找某個特定的節點或者包含某個指定的值的節點。以方括號括起。
例如:
路徑表達式 結果
/bookstore/book[1] 選取屬於 bookstore 子元素的第一個 book 元素。
/bookstore/book[last()] 選取屬於 bookstore 子元素的最後一個 book 元素。
/bookstore/book[last()-1] 選取屬於 bookstore 子元素的倒數第二個 book 元素。
/bookstore/book[position()<3] 選取最前面的兩個屬於 bookstore 元素的子元素的 book 元素。
//title[@lang] 選取所有擁有名爲 lang 的屬性的 title 元素。
//title[@lang='eng'] 選取所有 title 元素,且這些元素擁有值爲 eng 的 lang 屬性。
/bookstore/book[price>35.00] 選取所有 bookstore 元素的 book 元素,且其中的 price 元素的值須大於 35.00。
/bookstore/book[price>35.00]/title 選取所有 bookstore 元素中的 book 元素的 title 元素,且其中的 price 元素的值須大於 35.00。
 
四、通配符
通配符 描述
* 匹配任何元素節點
@* 匹配任何屬性節點
node() 匹配任何類型的節點
|          選取若干路徑  
 
例如:
路徑表達式 結果
/bookstore/* 選取 bookstore 元素的所有子節點
//* 選取文檔中的所有元素
//title[@*] 選取所有帶有屬性的 title 元素。

//book/title | //book/price 選取所有 book 元素的 tilte 和 price 元素。
//title | //price 選取所有文檔中的 title 和 price 元素。
/bookstore/book/title | //price 選取所有屬於 bookstore 元素的 book 元素的 title 元素,以及文檔中所有的 price 元素。
 
五、函數
名稱 結果
ancestor 選取當前節點的所有先輩(父、祖父等)
ancestor-or-self 選取當前節點的所有先輩(父、祖父等)以及當前節點本身
attribute 選取當前節點的所有屬性
child 選取當前節點的所有子元素。
descendant 選取當前節點的所有後代元素(子、孫等)。
descendant-or-self 選取當前節點的所有後代元素(子、孫等)以及當前節點本身。
following 選取文檔中當前節點的結束標籤之後的所有節點。
namespace 選取當前節點的所有命名空間節點
parent 選取當前節點的父節點。
preceding 選取文檔中當前節點的開始標籤之前的所有節點。
preceding-sibling 選取當前節點之前的所有同級節點。
self 選取當前節點。
路徑表達式可以是絕對路徑,也可以是相對路徑。例如:

絕對位置路徑:

/step/step/...

相對位置路徑:

step/step/...
其中的每一步又可以是一個表達式,包括:
軸(函數)(axis)
定義所選節點與當前節點之間的樹關係
節點測試(node-test)
識別某個軸內部的節點
零個或者更多謂語(predicate)
更深入地提煉所選的節點集
例如:
例子 結果
child::book 選取所有屬於當前節點的子元素的 book 節點
attribute::lang 選取當前節點的 lang 屬性
child::* 選取當前節點的所有子元素
attribute::* 選取當前節點的所有屬性
child::text() 選取當前節點的所有文本子節點
child::node() 選取當前節點的所有子節點
descendant::book 選取當前節點的所有 book 後代
ancestor::book 選擇當前節點的所有 book 先輩
ancestor-or-self::book 選取當前節點的所有book先輩以及當前節點(假如此節點是book節點的話)
child::*/child::price 選取當前節點的所有 price 孫。
 
六、運算符
運算符 描述 實例 返回值
| 計算兩個節點集 //book | //cd 返回所有帶有 book 和 ck 元素的節點集
+ 加法 6 + 4 10
- 減法 6 - 4 2
* 乘法 6 * 4 24
div 除法 8 div 4 2
= 等於 price=9.80 如果 price 是9.80,則返回 true。 如果 price 是9.90,則返回 fasle。
!= 不等於 price!=9.80 如果 price 是 9.90,則返回 true。 如果 price 是 9.98,則返回 fasle。
< 小於 price<9.80 如果price是9.00,則返回true 如果price是9.98,則返回fasle
<= 小於或等於 price<=9.80 如果 price 是9.00,則返回 true。 如果 price 是9.90,則返回 fasle。
> 大於 price>9.80 如果 price 是 9.90,則返回 true。 如果 price 是 9.80,則返回 fasle。
>= 大於或等於 price>=9.80 如果 price 是 9.90,則返回 true。 如果 price 是 9.70,則返回 fasle。
or price=9.80 or price=9.70 如果 price 是 9.80,則返回 true。 如果 price 是 9.50,則返回 fasle。
and price>9.00 and price<9.90 如果 price 是 9.80,則返回 true。 如果 price 是 8.50,則返回 fasle。
mod 計算除法的餘數 5 mod 2 1
 
七、在Java中使用Xpath
在java1.5中推出了一個javax.xml.xpath包專門用來在java中使用Xpath表達式來讀取xml。
1. 數據類型
在學習之前首先需要注意的是:Xpath的數據並不與Java有一一對應關係,Xpath1.0只聲明瞭四種數據類型:
  • node-set
  • number
  • boolean
  • string
     
    對應到java就是:
  • number 映射爲 java.lang.Double
  • string 映射爲 java.lang.String
  • boolean 映射爲 java.lang.Boolean
  • node-set 映射爲 org.w3c.dom.NodeList
     
    因此,在使用java的xpathAPI時,需要注意返回類型:
    Java代碼
    [java] view plaincopy
     
     
    1.  public Object evaluate(Object item, QName returnType)throws XPathExpressionException;    
    2.     
    3. public String evaluate(Object item)throws XPathExpressionException;    
    4.     
    5. public Object evaluate(InputSource source, QName returnType)throws XPathExpressionException;    
    6.    
    7. public String evaluate(InputSource source)throws XPathExpressionException;    
    [java] view plaincopy
     
     
    1. public Object evaluate(Object item, QName returnType)throws XPathExpressionException;  
    2.   
    3. public String evaluate(Object item)throws XPathExpressionException;  
    4.   
    5. public Object evaluate(InputSource source, QName returnType)throws XPathExpressionException;  
    6.   
    7. public String evaluate(InputSource source)throws XPathExpressionException;  
    不指定返回類型時,缺省返回類型爲String。指定返回類型時,需要把返回值由Object類型強制轉換成對應的返回類型。  
    2. API的使用
    類似於Dom,要得到一個Xpath對象,可以如下使用: Java代碼
    [java] view plaincopy
     
     
    1. XPathFactory factory = XPathFactory.newInstance();    
    2. XPath xpath = factory.newXPath();    
    3. XPathExpression expression = xpath.compile("/bookstore//book/title/text()");    
    [java] view plaincopy
     
     
    1. <strong><strong>        XPathFactory factory = XPathFactory.newInstance();  
    2.         XPath xpath = factory.newXPath();  
    3.         XPathExpression expression = xpath.compile("/bookstore//book/title/text()");</strong></strong>  
    還是以之前的xml文檔爲例。要得到這個表達式的結果,我們先要得到一個輸入對象,例如一個document:
    Java代碼
    [java] view plaincopy
     
     
    1. DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();    
    2. DocumentBuilder documentBuilder = builderFactory.newDocumentBuilder();    
    3. Document document = documentBuilder.parse(new File("books.xml"));    
    4. NodeList list = (NodeList) expression.evaluate(document,XPathConstants.NODESET);    
    [java] view plaincopy
     
     
    1. <strong><strong>        DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();  
    2.         DocumentBuilder documentBuilder = builderFactory.newDocumentBuilder();  
    3.         Document document = documentBuilder.parse(new File("books.xml"));  
    4.         NodeList list = (NodeList) expression.evaluate(document,XPathConstants.NODESET);</strong></strong>  
    這裏可以看出,在使用Xpath的時候,我們好像需要很清楚的知道返回結果是什麼。否則就不能得到意想的結果。 
    最後,我們得到一個title的list值:
    Java代碼
    [java] view plaincopy
     
     
    1. for(int i = 0;i<list.getLength();i++){                System.out.println(list.item(i).getNodeValue());    
    2. }  
    [java] view plaincopy
     
     
    1. <strong><strong>        for(int i = 0;i</strong></strong>  
    Java代碼 
    1. Everyday Italian  
    2. Harry Potter  
    3. XQuery Kick Start  
    4. Learning XML  
    [java] view plaincopy
     
     
    1. <strong><strong>Everyday Italian  
    2. Harry Potter  
    3. XQuery Kick Start  
    4. Learning XML</strong></strong>  
八、處理命令空間
一般一個規範xml都會有命名空間的定義,例如:
[xml] view plaincopy
 
 
  1. <strong><strong>  
  2.   
  3.             
  4.             Hello  
  5.             
  6. </strong></strong>  
 
[java] view plaincopy
 
 
  1. <?xml version="1.0" encoding="UTF-8"?>    
  2. <tg:bookstore xmlns:tg="http://www.tibco.com/cdc/liugang"    
  3.            xmlns:ns="http://www.tibco.com/cdc/liugang/ns">    
  4.           <ns:book>    
  5.             <tg:title>Hello</tg:title>    
  6.           </ns:book>    
  7. </tg:bookstore>  
 
xpath中定義了與節點名和命名空間有關的三個函數:
  • local-name()
  • namespace-uri()
  • name()
例如要查找所有在當前文檔中定義的,元素的local名爲book的結點,則如下:
Java代碼
[java] view plaincopy
 
 
  1. XPathFactory xPathFactory = XPathFactory.newInstance();    
  2. XPath xpath = xPathFactory.newXPath();    
  3. XPathExpression compile = xpath.compile("//*[local-name()='book']");    
  4. NodeList list = (NodeList) compile.evaluate(document,XPathConstants.NODESET);    
[java] view plaincopy
 
 
  1. <strong><strong>        XPathFactory xPathFactory = XPathFactory.newInstance();  
  2.         XPath xpath = xPathFactory.newXPath();  
  3.         XPathExpression compile = xpath.compile("//*[local-name()='book']");  
  4.         NodeList list = (NodeList) compile.evaluate(document,XPathConstants.NODESET);</strong></strong>  
如果元素定義了命名空間,則使用xpath查找時也必須指定在同一個命名空間中,即便元素使用的是缺省的命名空間,剛查找也需要定義缺省的命名空間。 例如文檔:
Xml代碼
[xhtml] view plaincopy
 
 
  1. <?xml version="1.0" encoding="UTF-8"?>    
  2. <bookstore xmlns="http://www.tibco.com/cdc/liugang" xmlns:tg="http://www.tibco.com/cdc/liugang/tg"    
  3.            xmlns:ns="http://www.tibco.com/cdc/liugang/ns">    
  4.           <ns:book>    
  5.             <tg:title>Hello</tg:title>    
  6.           </ns:book>    
  7.           <computer>    
  8.                <id>ElsIOIELdslke-1233</id>    
  9.           </computer>    
  10. </bookstore>  
[xml] view plaincopy
 
 
  1. <strong><strong>  
  2.   
  3.             
  4.             Hello  
  5.             
  6.             
  7.                ElsIOIELdslke-1233  
  8.             
  9. </strong></strong>  
定義了三個命名空間:缺省的;xmlns:tg;xmlns:ns。 要使用命名空間,我們需要設置XPath的命名空間上下文:NamespaceContext。這是一個接口類型,我們需要自定義去實現它。例如對應於上文檔的三個命名空間,可以如下實現:
Java代碼
[java] view plaincopy
 
 
  1. class CustomNamespaceContext implements NamespaceContext{    
  2.     
  3.         public String getNamespaceURI(String prefix) {    
  4.             if(prefix.equals("ns")){    
  5.                 return "http://www.tibco.com/cdc/liugang/ns";    
  6.             }else if(prefix.equals("tg")){    
  7.                 return "http://www.tibco.com/cdc/liugang/tg";    
  8.             }else if(prefix.equals("df")){    
  9.                 return "http://www.tibco.com/cdc/liugang";    
  10.             }    
  11.             return XMLConstants.NULL_NS_URI;    
  12.         }    
  13.     
  14.         public String getPrefix(String namespaceURI) {    
  15.             return null;    
  16.         }    
  17.     
  18.         public Iterator getPrefixes(String namespaceURI) {    
  19.             return null;    
  20.         }    
  21.             
  22.     }    
[java] view plaincopy
 
 
  1. <strong><strong>class CustomNamespaceContext implements NamespaceContext{  
  2.   
  3.         public String getNamespaceURI(String prefix) {  
  4.             if(prefix.equals("ns")){  
  5.                 return "http://www.tibco.com/cdc/liugang/ns";  
  6.             }else if(prefix.equals("tg")){  
  7.                 return "http://www.tibco.com/cdc/liugang/tg";  
  8.             }else if(prefix.equals("df")){  
  9.                 return "http://www.tibco.com/cdc/liugang";  
  10.             }  
  11.             return XMLConstants.NULL_NS_URI;  
  12.         }  
  13.   
  14.         public String getPrefix(String namespaceURI) {  
  15.             return null;  
  16.         }  
  17.   
  18.         public Iterator getPrefixes(String namespaceURI) {  
  19.             return null;  
  20.         }  
  21.           
  22.     }</strong></strong>  
方法名都非常直觀。這裏只實現第一個方法。 這樣,如果要查找命名空間是缺省,元素名爲computer的所有元素,可以如下實現:
Java代碼
[java] view plaincopy
 
 
  1. XPathFactory xPathFactory = XPathFactory.newInstance();    
  2. XPath xpath = xPathFactory.newXPath();    
  3. xpath.setNamespaceContext(new CustomNamespaceContext());    
  4. XPathExpression compile = xpath.compile("//df:computer");    
  5. NodeList list = (NodeList) compile.evaluate(document,XPathConstants.NODESET);    
  6. for(int i = 0;i  
  7.     Node item = list.item(i);    
  8.     System.out.println(item.getNodeName()+"  "+item.getNodeValue());    
  9. }    
[java] view plaincopy
 
 
  1. <strong><strong>        XPathFactory xPathFactory = XPathFactory.newInstance();  
  2.         XPath xpath = xPathFactory.newXPath();  
  3.         xpath.setNamespaceContext(new CustomNamespaceContext());  
  4.         XPathExpression compile = xpath.compile("//df:computer");  
  5.         NodeList list = (NodeList) compile.evaluate(document,XPathConstants.NODESET);  
  6.         for(int i = 0;i</strong></strong>  
  九、其他
除此之外,在java中,還可以定義擴展的函數解釋器和變量解釋器,看XPath的方法:
Java代碼
[java] view plaincopy
 
 
  1.     /**  
  2.      *  
  3. Establish a variable resolver. 
  4.   
  5.      *   
  6.      *  
  7. A NullPointerException is thrown if resolver is null. 
  8.   
  9.      *   
  10.      * @param resolver Variable resolver.  
  11.      *   
  12.      *  @throws NullPointerException If resolver is null.  
  13.      */    
  14.     public void setXPathVariableResolver(XPathVariableResolver resolver);    
  15.     
  16.     
  17.     /**  
  18.        *  
  19. Establish a function resolver. 
  20.   
  21.        *   
  22.        *  
  23. A NullPointerException is thrown if resolver is null. 
  24.   
  25.        *   
  26.        * @param resolver XPath function resolver.  
  27.        *   
  28.        * @throws NullPointerException If resolver is null.  
  29.        */    
  30.     public void setXPathFunctionResolver(XPathFunctionResolver resolver);   
[java] view plaincopy
 
 
  1. <strong><strong>    /** 
  2.      * Establish a variable resolver. 
  3.      *  
  4.      * A <code>NullPointerException</code> is thrown if <code>resolver</code> is <code>null</code>. 
  5.      *  
  6.      * @param resolver Variable resolver. 
  7.      *  
  8.      *  @throws NullPointerException If <code>resolver</code> is <code>null</code>. 
  9.      */  
  10.     public void setXPathVariableResolver(XPathVariableResolver resolver);  
  11.   
  12.   
  13.     /** 
  14.        * Establish a function resolver. 
  15.        *  
  16.        * A <code>NullPointerException</code> is thrown if <code>resolver</code> is <code>null</code>. 
  17.        *  
  18.        * @param resolver XPath function resolver. 
  19.        *  
  20.        * @throws NullPointerException If <code>resolver</code> is <code>null</code>. 
  21.        */  
  22.     public void setXPathFunctionResolver(XPathFunctionResolver resolver);</strong>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章