Notes: XML-parsing document-XPath positioning information, xml-xpath
If you need to locate a specific piece of information in an XML document, it is a bit difficult to search rows by traversing many nodes in the DOM tree. The XPath language makes it easy to access Tree nodes, for example, the following XML document structure:
<? Xmlversion = "1.0" encoding = "UTF-8"?>
<Root>
<Title>
<Fontenabled = "false">
<! -- Font name -->
<Name> Helvetica </name>
<Size>36</Size>
</Font>
<Data>
<! [CDATA [xml document root node <root.../>]>
</Data>
</Title>
</Root>
You can evaluate the XPath expression/root/title/font/size to obtain the value of the size node. The sample code is as follows:
DocumentBuilderFactory factory = DocumentBuilderFactory. newInstance ();
DocumentBuilder builder = factory. newDocumentBuilder ();
Path xmlPath = Paths. get ("E :\\ IDEA Workspace \ exampleiostream \ src \ main \ java \ org \ drsoft \ examples \ xml", "appParse. xml ");
InputStream xmlStream = Files. newInputStream (xmlPath, StandardOpenOption. READ );
Document xmlDocument = builder. parse (xmlStream );
XPathFactory xPathFactory = XPathFactory. newInstance ();
XPath xPath = xPathFactory. newXPath ();
String xpathResult = xPath. evaluate ("/root/title/font/size", xmlDocument );
System. out. println ("XPath/root/title/font/size value" + xpathResult );
XPath expressions also provide richer syntax to filter nodes for XML documents. nodes are selected by following the path or step (steps:
Expression |
Description |
Example expression |
Result |
Nodename |
Select all child nodes of the node. |
Root |
Select the node of the root element |
/ |
Select from the root node. |
/Root |
Select the root element. Note: If the path starts with a forward slash (/), the path always represents the absolute path to an element. |
// |
Select the nodes in the document from the current node that matches the selected node, regardless of their location. |
// Title |
Select All title child elements, regardless of their location in the document |
. |
Select the current node. |
|
|
.. |
Select the parent node of the current node. |
|
|
@ |
Select attributes. |
// @ Enabled |
Select all attributes named enabled |
|
|
|
|
The sample code is as follows:
NodeList list = (NodeList) xPath. evaluate ("root", xmlDocument, XPathConstants. NODESET );
For (int I =0; I <list. getLength (); I ++ ){
System. out. println ("XPath root Node" + list. item (I). getNodeName ());
}
System. out. println ("----------------------------------------");
List = (NodeList) xPath. evaluate ("// title", xmlDocument, XPathConstants. NODESET );
For (int I =0; I <list. getLength (); I ++ ){
System. out. println ("XPath // title Node" + list. item (I). getNodeName ());
}
System. out. println ("----------------------------------------");
List = (NodeList) xPath. evaluate ("// @ enabled", xmlDocument, XPathConstants. NODESET );
For (int I =0; I <list. getLength (); I ++ ){
System. out. println ("XPath // @ enabled Node" + list. item (I ). getNodeName () + "Value" + list. item (I ). getNodeValue ());
}
System. out. println ("----------------------------------------");
- Predicates:A predicate is used to find a specific node or a node that contains a specified value. It is embedded in square brackets.
Path expression |
Result |
/Root/title [1] |
Select the first title element that belongs to the root sub-element. |
/Root/title [last ()] |
Select the last title element that belongs to the root sub-element. |
/Root/title [last ()-1] |
Select the penultimate title element that belongs to the root sub-element. |
/Root/title [position () <3] |
Select the title element of the first two child elements of the root element. |
// Font [@ enabled] |
Select All font elements with the attribute "enabled. |
// Font [@ enabled = 'true'] |
Select All font elements, and these elements have the enabled attribute with the value true. |
/Root/title/font [size> 35] |
Select all the font elements of the title under the root element, and the value of the size element must be greater than 35. |
/Root/title/font [size> 35]/name |
Select All name elements under the font element of the title under the root element, and the value of the size element must be greater than 35 |
The sample code is as follows:
List = (NodeList) xPath. evaluate ("/root/title/font [size> 10]", xmlDocument, XPathConstants. NODESET );
For (int I =0; I <list. getLength (); I ++ ){
System. out. println ("XPath/root/title/font [size> 10] Node" + list. item (I). getNodeName ()
+ "Value" + list. item (I). getNodeValue ());
}
System. out. println ("----------------------------------------");
List = (NodeList) xPath. evaluate ("/root/title/font [size> 10]/name", xmlDocument, XPathConstants. NODESET );
For (int I =0; I <list. getLength (); I ++ ){
System. out. println ("XPath/root/title/font [size> 10]/name Node" + list. item (I). getNodeName ()
+ "Value" + list. item (I). getNodeValue ());
}
System. out. println ("----------------------------------------");
Operator |
Description |
Instance |
Return Value |
| |
Calculate two node sets |
// Book | // cd |
Returns all node sets with book and cd elements. |
+ |
Addition |
6 + 4 |
10 |
- |
Subtraction |
6-4 |
2 |
* |
Multiplication |
6*4 |
24 |
Div |
Division |
8 div 4 |
2 |
= |
Equal |
Price = 9.80 |
If the price is 9.80, true is returned. If the price is 9.90, false is returned. |
! = |
Not equal |
Price! = 9.80 |
If the price is 9.90, true is returned. If the price is 9.80, false is returned. |
< |
Less |
Price <1, 9.80 |
If the price is 9.00, true is returned. If the price is 9.90, false is returned. |
<= |
Less than or equal |
Price <= 9.80 |
If the price is 9.00, true is returned. If the price is 9.90, false is returned. |
> |
Greater |
Price> 9.80 |
If the price is 9.90, true is returned. If the price is 9.80, false is returned. |
> = |
Greater than or equal |
Price >=9.80 |
If the price is 9.90, true is returned. If the price is 9.70, false is returned. |
Or |
Or |
Price = 9.80 or price = 9.70 |
If the price is 9.80, true is returned. If the price is 9.50, false is returned. |
And |
And |
Price> 9.00 and price <9.90 |
If the price is 9.80, true is returned. If the price is 8.50, false is returned. |
Mod |
Calculate the remainder of the Division |
5 mod 2 |
1 |
Name |
Description |
Context Functions |
|
Position () |
Returns the index position of the node being processed. Example: // book [position () <= 3] Result: select the first three book elements. |
Last () |
Returns the number of projects in the List of processed nodes. Example: // book [last ()] Result: select the last book element. |
Aggregate functions |
|
Count |
Number of returned nodes |
Sum |
Returns the sum of the values of each node in the specified node set. |
String Functions |
|
String |
Returns the string value of the parameter. Parameters can be numbers, logical values, or node sets. For example, string (314) Result: "314" |
Concat |
Returns the concatenation of strings. Example: concat ('xpath ', 'ais', 'fun! ') Result: 'xpath is FUN! ' |
Substring |
Returns a substring of the specified length starting from the start position. The subscript of the first character is 1. If the len parameter is omitted, the substring from the position start to the end of the string is returned. Example: substring ('Beatles) Result: 'beat' Example: substring ('Beatles ', 2) Result: 'eates' |
String-length |
Returns the length of the specified string. If the string parameter is not set, the length of the string value of the current node is returned. Example: string-length ('Beatles ') Result: 7 |
Other functions |
|
Number |
Returns the value of the parameter. The parameter can be a Boolean value, string, or node set. Example: number ('123 ') Result: 100 |
Round |
Round the num parameter to the nearest integer. Example: round (3.14) Result: 3 Example: round (3.56) Result: 4 |
Floor |
Returns the maximum integer not greater than the num value. Example: floor (3.14) Result: 3 |
Ceiling |
Returns the smallest integer greater than the num parameter. Example: ceiling (3.14) Result: 4 |
The sample code is as follows:
XpathResult = xPath. evaluate ("count (// title)", xmlDocument );
System. out. println ("XPath expression count (// title) value =" + xpathResult );
XpathResult = xPath. evaluate ("sum (// size/text ()", xmlDocument );
System. out. println ("XPath expression sum (// size/text () value =" + xpathResult );
XpathResult = xPath. evaluate ("// title [position () = 2]/font/size/text ()", xmlDocument );
System. out. println ("XPath expression // title [position () = 2]/font/size/text () value =" + xpathResult );
XpathResult = xPath. evaluate ("concat (/root/title/font/name, root/title/font/size)", xmlDocument );
System. out. println ("XPath expression concat (/root/title/font/name, root/title/font/size) value =" + xpathResult );
XpathResult = xPath. evaluate ("concat (3.14), round (3.56), floor (3.14), floor (3.56)", xmlDocument );
System. out. println ("XPath expression concat (round (3.14), round (3.56), floor (3.14), floor (3.56) value =" + xpathResult );