In-depth explanation of XPath and Java sample code analysis

Source: Internet
Author: User
Tags xquery

Copy codeThe Code is as follows: 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 ());
}
}
}

I. node types
There are seven node types in XPath: elements, attributes, text, namespaces, processing commands, comments, and document nodes (or become root nodes ). The root node of the document is the document node. The corresponding attribute has the attribute node and the element has the element node.
Ii. Common Path expressions
Expression description
Nodename: Select All subnodes of this node
/Select from the root node
// Select the nodes in the document from the current node that matches the selected node, regardless of their location
. Select the current node
.. Select the parent node of the current node
@ Select attributes
For example, there is a document:Copy codeThe Code is as follows: <? 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>

Then:
Path expression result
Bookstore Selects all subnodes of the bookstore Element
/Bookstore select the root element bookstore Note: If the path starts with a forward slash (/), this path always represents the absolute path to an element!
Bookstore/book Selects all the book elements that belong to the sub-elements of bookstore.
// Book Selects all the child elements of the book, regardless of their location in the document.
Bookstore // book Selects all the book elements belonging to the descendants of the bookstore element, regardless of their location under the bookstore.
// @ Lang select all attributes named lang.
Iii. Restrictions
It is used to find a specific node or a node that contains a specified value. Enclosed in square brackets.
For example:
Path expression result
/Bookstore/book [1] select the first book element that belongs to the bookstore sub-element.
/Bookstore/book [last ()] select the last book element that belongs to the bookstore sub-element.
/Bookstore/book [last ()-1] select the last and second book elements belonging to the bookstore sub-elements.
/Bookstore/book [position () <3] select the first two bookstore sub-elements.
// Title [@ lang] Selects all the title elements with the attribute lang.
// Title [@ lang = 'eng'] Selects all title elements and these elements have the lang attribute with the value of eng.
/Bookstore/book [price> 35.00] selects the book element of all bookstore elements, and the value of the price element must be greater than 35.00.
/Bookstore/book [price> 35.00]/title: select the title element of the book element in all bookstore elements, and the value of the price element must be greater than 35.00.
4. wildcard characters
Wildcard description
* Match any element node
@ * Match any attribute node
Node () matches any type of node
| Select several paths
For example:
Path expression result
/Bookstore/* select all subnodes of the bookstore Element
// * Select all elements in the document
// Title [@ *] Selects all the title elements with attributes.
// Book/title | // book/price select the tilte and price elements of all book elements.
// Title | // price select the title and price elements in all documents.
/Bookstore/book/title | // price select the title element of all the book elements that belong to the bookstore element, and all the price elements in the document.

V. Functions
Name result
Ancestor Selects all the ancestors of the current node (parent, grandfather, etc)
Ancestor-or-self Selects all the ancestors of the current node (parent, grandfather, etc.) and the current node itself
Attribute Selects all attributes of the current node
Child Selects all child elements of the current node.
Descendant Selects all descendant elements (child, sun, etc.) of the current node ).
Descendant-or-self Selects all child elements (child, sun, etc.) of the current node and the current node itself.
Following Selects all nodes after the end label of the current node in the document.
Namespace: Select All namespace nodes of the current node
Parent selects the parent node of the current node.
Preceding Selects all nodes before the start label of the current node in the document.
Preceding-sibling Selects all peer nodes before the current node.
Self selects the current node.
The path expression can be an absolute or relative path. For example:
Absolute path:
/Step/... relative path:
Step/... each step can be an expression, including:
Axis)
Define the tree relationship between the selected node and the current node
Node-test)
Recognizes nodes inside a certain axis
Zero or more predicates (predicate)
Further refine the selected node set
Example: Result
Child: book Selects all the book nodes that belong to the child elements of the current node.
Attribute: lang selects the lang attribute of the current node.
Child: * select all child elements of the current node.
Attribute: * select all attributes of the current node.
Child: text () select all text subnodes of the current node
Child: node () selects all child nodes of the current node
Descendant: book Selects all book descendants of the current node
Ancestor: book Selects all the books of the current node.
Ancestor-or-self: book Selects all the book predecessors of the current node and the current node (if this node is a book node)
Child: */child: price: select all the price grandchildren of the current node.
Vi. Operators
Operator description instance Return Value
| Calculate two node sets // book | // cd returns all node sets with book and ck Elements
+ Addition 6 + 4 10
-Subtraction 6-4 2
* Multiplication 6*4 24
Div Division 8 div 4 2
= Equal to price = 9.80 if price is 9.80, true is returned. If the price is 9.90, return fasle.
! = Not equal to price! = 9.80 if the price is 9.90, true is returned. If the price is 9.98, return fasle.
<Less than price <9.80 if price is 9.00, true is returned. If price is 9.98, fasle is returned.
<= Less than or equal to price <= 9.80 if price is 9.00, true is returned. If the price is 9.90, return fasle.
> Greater than price> 9.80 if price is 9.90, true is returned. If the price is 9.80, return fasle.
>=Greater than or equal to price >=9.80 if price is 9.90, true is returned. If the price is 9.70, return fasle.
Or price = 9.80 or price = 9.70 if the price is 9.80, true is returned. If the price is 9.50, return fasle.
And price> 9.00 and price <9.90 if the price is 9.80, true is returned. If the price is 8.50, return fasle.
Mod calculates the remainder of division 5 mod 2 1

7. Use Xpath in Java
Java introduces a javax. xml. xpath package specifically used to read xml using Xpath expressions in java. 1. Data Type
Before learning, note that the data in Xpath does not have a one-to-one correspondence with Java. Xpath1.0 only declares four data types:
• Node-set
• Number
• Boolean
• String

Corresponding to java:
• Number ing to java. lang. Double
• String ing to java. lang. String
• Boolean ing to java. lang. Boolean
• Node-set ing is org. w3c. dom. NodeList

Therefore, when using java xpathAPI, pay attention to the return type:
Java codeCopy codeThe Code is as follows: 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;

Copy codeThe Code is as follows: 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;

If no return type is specified, the default return type is String. When specifying the return type, you must forcibly convert the return value from the Object type to the corresponding return type.
Use of Apis
Similar to Dom, to get an Xpath object, you can use the following code: JavaCopy codeThe Code is as follows: XPathFactory factory = XPathFactory. newInstance ();
XPath xpath = factory. newXPath ();
XPathExpression expression = xpath. compile ("/bookstore // book/title/text ()");

Copy codeThe Code is as follows: <strong> XPathFactory factory = XPathFactory. newInstance ();
XPath xpath = factory. newXPath ();
XPathExpression expression = xpath. compile ("/bookstore // book/title/text ()"); </strong>

Take the preceding xml document as an example. To get the result of this expression, we first need to get an input object, such as a document:Copy codeThe Code is as follows: DocumentBuilderFactory builderFactory = DocumentBuilderFactory. newInstance ();
DocumentBuilder documentBuilder = builderFactory. newDocumentBuilder ();
Document document = documentBuilder. parse (new File ("books. xml "));
NodeList list = (NodeList) expression. evaluate (document, XPathConstants. NODESET );

Copy codeThe Code is as follows: <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>

We can see that when using Xpath, we need to clearly know what the returned result is. Otherwise, you cannot get the expected results.
Finally, we get the list Value of a title:Copy codeThe Code is as follows: for (int I = 0; I <list. getLength (); I ++) {System. out. println (list. item (I ). getNodeValue ());
}

Copy codeThe Code is as follows: <strong> for (int I = 0; I </strong>

Copy codeThe Code is as follows: Everyday Italian
Harry Potter
XQuery Kick Start
Learning XML

Copy codeThe Code is as follows: <strong> Everyday Italian
Harry Potter
XQuery Kick Start
Learning XML </strong>

8. to process a command space, a standard xml will have a namespace definition, for example:
Copy codeThe Code is as follows: <strong>

Hello

</Strong>

Copy codeThe Code is as follows: <? 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>

Xpath defines three functions related to the node name and namespace:
• Local-name ()
• Namespace-uri ()
• Name ()
For example, to search for all the nodes defined in the current document, the local name of the element is book, as follows:Copy codeThe Code is as follows: XPathFactory xPathFactory = XPathFactory. newInstance ();
XPath xpath = xPathFactory. newXPath ();
XPathExpression compile = xpath. compile ("// * [local-name () = 'book']");
NodeList list = (NodeList) compile. evaluate (document, XPathConstants. NODESET );

Copy codeThe Code is as follows: <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>

If an element defines a namespace, you must specify it in the same namespace when using xpath for search, even if the element uses the default namespace, you also need to define the default namespace for the search. Example:Copy codeThe Code is as follows: <? Xml version = "1.0" encoding = "UTF-8"?>
<Bookstore xmlns = "http://www.tibco.com/cdc/liugang" xmlns: tg = "http://www.tibco.com/cdc/liugang/tg"
Xmlns: ns = "http://www.tibco.com/cdc/liugang/ns">
<Ns: book>
<Tg: title> Hello </tg: title>
</Ns: book>
<Computer>
ElsIOIELdslke-1233
</Computer>
</Bookstore>

Copy codeThe Code is as follows: <strong>

Hello

ElsIOIELdslke-1233

</Strong>

Three namespaces are defined: default; xmlns: tg; xmlns: ns. To use a namespace, we need to set the namespace context of XPath: NamespaceContext. This is an interface type, which needs to be customized. For example, the three namespaces corresponding to the preceding document can be implemented as follows:Copy codeThe Code is as follows: 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;
}

}

Copy codeThe Code is as follows: <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>

Method names are intuitive. Only the first method is implemented here. In this way, if you want to find that the namespace is the default, all elements named computer can be implemented as follows:Copy codeThe Code is as follows: 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
Node item = list. item (I );
System. out. println (item. getNodeName () + "" + item. getNodeValue ());
}

Copy codeThe Code is as follows: <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>

9. Others
In addition, in java, you can also define extended function interpreters and variable interpreters. See the XPath method:Copy codeThe Code is as follows :/**
*
Establish a variable resolver.

*
*
A NullPointerException is thrown if resolver is null.

*
* @ Param resolver Variable resolver.
*
* @ Throws NullPointerException If resolver is null.
*/
Public void setXPathVariableResolver (XPathVariableResolver resolver );

/**
*
Establish a function resolver.

*
*
A NullPointerException is thrown if resolver is null.

*
* @ Param resolver XPath function resolver.
*
* @ Throws NullPointerException If resolver is null.
*/
Public void setXPathFunctionResolver (XPathFunctionResolver resolver );

Copy codeThe Code is as follows: <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>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.