- import java.io.IOException;
- import javax.xml.parsers.*;
- import javax.xml.xpath.*;
- import org.w3c.dom.*;
- import org.xml.sax.SAXException;
- public class XpathTest {
- public static void main(String[] args) throws ParserConfigurationException,
- SAXException, IOException, XPathExpressionException {
- DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
- factory.setNamespaceAware(false);
- DocumentBuilder builder = factory.newDocumentBuilder();
- Document doc = builder.parse("C:/Users/Administrator/Desktop/test.xml");
- System.out.println(doc.getChildNodes().getLength());
- XPathFactory xFactory = XPathFactory.newInstance();
- XPath xpath = xFactory.newXPath();
- XPathExpression expr = xpath
- .compile("//name/text()");
- Object result = expr.evaluate(doc, XPathConstants.NODESET);
- NodeList nodes = (NodeList) result;
- System.out.println(nodes.getLength());
- for (int i = 0; i < nodes.getLength(); i++) {
- System.out.println(nodes.item(i).getNodeValue());
- }
- }
- }
表達式 | 描述 |
---|---|
nodename | 選取此節點的所有子節點 |
/ | 從根節點選取 |
// | 從匹配選擇的當前節點選擇文檔中的節點,而不考慮它們的位置 |
. | 選取當前節點 |
.. | 選取當前節點的父節點 |
@ | 選取屬性 |
- <?xml version="1.0" encoding="ISO-8859-1"?>
- <bookstore>
- <book>
- <title lang="eng">Harry Potter</title>
- <price>29.99</price>
- </book>
- <book>
- <title lang="eng">Learning XML</title>
- <price>39.95</price>
- </book>
- </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 |
- 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代碼- public Object evaluate(Object item, QName returnType)throws XPathExpressionException;
- public String evaluate(Object item)throws XPathExpressionException;
- public Object evaluate(InputSource source, QName returnType)throws XPathExpressionException;
- public String evaluate(InputSource source)throws XPathExpressionException;
- public Object evaluate(Object item, QName returnType)throws XPathExpressionException;
- public String evaluate(Object item)throws XPathExpressionException;
- public Object evaluate(InputSource source, QName returnType)throws XPathExpressionException;
- public String evaluate(InputSource source)throws XPathExpressionException;
不指定返回類型時,缺省返回類型爲String。指定返回類型時,需要把返回值由Object類型強制轉換成對應的返回類型。2. API的使用類似於Dom,要得到一個Xpath對象,可以如下使用: Java代碼- <strong><strong> XPathFactory factory = XPathFactory.newInstance();
- XPath xpath = factory.newXPath();
- XPathExpression expression = xpath.compile("/bookstore//book/title/text()");</strong></strong>
- <strong><strong> DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
- DocumentBuilder documentBuilder = builderFactory.newDocumentBuilder();
- Document document = documentBuilder.parse(new File("books.xml"));
- NodeList list = (NodeList) expression.evaluate(document,XPathConstants.NODESET);</strong></strong>
最後,我們得到一個title的list值:- <strong><strong> for(int i = 0;i</strong></strong>
- Everyday Italian
- Harry Potter
- XQuery Kick Start
- Learning XML
- <strong><strong>Everyday Italian
- Harry Potter
- XQuery Kick Start
- Learning XML</strong></strong>
- <strong><strong>
- Hello
- </strong></strong>
- <?xml version="1.0" encoding="UTF-8"?>
- <tg:bookstore xmlns:tg="http://www.tibco.com/cdc/liugang"
- xmlns:ns="http://www.tibco.com/cdc/liugang/ns">
- <ns:book>
- <tg:title>Hello</tg:title>
- </ns:book>
- </tg:bookstore>
- local-name()
- namespace-uri()
- name()
- <strong><strong> XPathFactory xPathFactory = XPathFactory.newInstance();
- XPath xpath = xPathFactory.newXPath();
- XPathExpression compile = xpath.compile("//*[local-name()='book']");
- NodeList list = (NodeList) compile.evaluate(document,XPathConstants.NODESET);</strong></strong>
- <strong><strong>
- Hello
- ElsIOIELdslke-1233
- </strong></strong>
- <strong><strong>class CustomNamespaceContext implements NamespaceContext{
- public String getNamespaceURI(String prefix) {
- if(prefix.equals("ns")){
- return "http://www.tibco.com/cdc/liugang/ns";
- }else if(prefix.equals("tg")){
- return "http://www.tibco.com/cdc/liugang/tg";
- }else if(prefix.equals("df")){
- return "http://www.tibco.com/cdc/liugang";
- }
- return XMLConstants.NULL_NS_URI;
- }
- public String getPrefix(String namespaceURI) {
- return null;
- }
- public Iterator getPrefixes(String namespaceURI) {
- return null;
- }
- }</strong></strong>
- <strong><strong> XPathFactory xPathFactory = XPathFactory.newInstance();
- XPath xpath = xPathFactory.newXPath();
- xpath.setNamespaceContext(new CustomNamespaceContext());
- XPathExpression compile = xpath.compile("//df:computer");
- NodeList list = (NodeList) compile.evaluate(document,XPathConstants.NODESET);
- for(int i = 0;i</strong></strong>
- <strong><strong> /**
- * Establish a variable resolver.
- *
- * A <code>NullPointerException</code> is thrown if <code>resolver</code> is <code>null</code>.
- *
- * @param resolver Variable resolver.
- *
- * @throws NullPointerException If <code>resolver</code> is <code>null</code>.
- */
- public void setXPathVariableResolver(XPathVariableResolver resolver);
- /**
- * Establish a function resolver.
- *
- * A <code>NullPointerException</code> is thrown if <code>resolver</code> is <code>null</code>.
- *
- * @param resolver XPath function resolver.
- *
- * @throws NullPointerException If <code>resolver</code> is <code>null</code>.
- */
- public void setXPathFunctionResolver(XPathFunctionResolver resolver);</strong>