Dom4j queries XML through xpath

Source: Internet
Author: User
  1. Get document

    • Saxreader = new saxreader ();
    • Document document = saxreader. Read (fileutil. getfileinputstream (filename ));
  2. Query Element
    • String XPath = "/Composites/composite [@ type = 'ondelete']"; // query the composite of the attribute type = 'ondelete'
    • List <element> composites = Document. selectnodes (XPath );
  3. XPath syntax
Select Node

XPath uses path expressions to select nodes in XML documents. Nodes are selected by following the path or step.

The most useful path expressions are listed below:
Expression Description
Nodename Select all child nodes of this node
/ Select from Root Node
// Select the nodes in the document from the current node that matches the selected node, regardless of their location
. Select current node
.. Select the parent node of the current node
@ Select attributes
Instance

In the following table, we have listed some path expressions and expression results:

Path expression Result
Bookstore Select All subnodes of the bookstore Element
/Bookstore

Select the root element bookstore

Note: If the path starts with a forward slash (/), the path always represents the absolute path to an element!

Bookstore/book Select the book elements of all the sub-elements of the bookstore.
// Book Select All book child elements regardless of their location in the document.
Bookstore // book Select the book elements of all future generations of the bookstore elements, regardless of their location under the bookstore.
// @ Lang Select all properties named Lang.
Predicates)

It is used to find a specific node or a node that contains a specified value.

The predicates are embedded in square brackets.

Instance

In the following table, we list some path expressions with predicates and the results of the expressions:

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 penultimate book element that belongs to the bookstore sub-element.
/Bookstore/book [position () <3] Select the first two bookstore sub-elements.
// Title [@ Lang] Select all the title elements with the lang attribute.
// Title [@ lang = 'eng'] Select All title elements and these elements have the lang attribute whose value is Eng.
/Bookstore/book [price> 35.00] Select the book element of all the 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 the bookstore elements, and the value of the price element must be greater than 35.00.
Select unknown Node

The XPath wildcard can be used to select unknown XML elements.

Wildcard Description
* Match any element node
@* Match any attribute node
Node () Match any type of Node
Instance

In the following table, we list some path expressions and the results of these expressions:

Path expression Result
/Bookstore /* Select All subnodes of the bookstore Element
//* Select all elements in the document
// Title [@ *] Select All title elements with attributes.
Select several paths

You can select several paths by using the "|" operator in the path expression.

Instance

In the following table, we list some path expressions and the results of these expressions:

Path expression Result
// Book/Title | // book/price Select the title and price 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.
XPath axis

The axis defines a node set relative to the current node.

Axis name Result
Ancestor Select all the ancestors of the current node (parent, grandfather, etc)
Ancestor-or-self Select all the founders of the current node (parent, grandfather, etc.) and the current node itself
Attribute Select all attributes of the current node
Child Select all child elements of the current node.
Descendant Select all descendant elements (child, sun, etc.) of the current node ).
Descendant-or-self Select all descendant elements (child, sun, etc.) of the current node and the current node itself.
Following Select all nodes after the end label of the current node in the document.
Namespace Select All namespace nodes of the current node
Parent Select the parent node of the current node.
Preceding Select all nodes before the start label of the current node in the document.
Preceding-sibling Select all peer nodes before the current node.
Self Select the current node.
Location Path expression

The location path can be absolute or relative.

The absolute path starts with a forward slash (/), but the relative path does not. In either case, the path contains one or more steps, and each step is separated by a slash:

Absolute path:
/step/step/...
Relative Path:
step/step/...

Each step is calculated based on the nodes in the current node set.

Steps include:
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
Step Syntax:
Axis name: node test [predicates]
Instance
Example Result
Child: Book Select the book nodes of all the child elements of the current node
Attribute: Lang Select 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 () Select All subnodes of the current node
Descendant: Book Select All book descendants of the current node
Ancestor: Book Select All book predecessors of the current node
Ancestor-or-self: Book Select all the book predecessors of the current node and the current node (if this node is a book node)
Child: */child: Price Select all price grandchildren of the current node.

XPath Operators

Operators available in XPath expressions are listed below:

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 Price = 9.80

If the price is 9.80, true is returned.

If the price is 9.90, return fasle.

! = Not equal Price! = 9.80

If the price is 9.90, true is returned.

If the price is 9.80, return fasle.

< Less Price <1, 9.80

If the price is 9.00, true is returned.

If the price is 9.90, return fasle.

<= Less than or equal Price <= 9.80

If the price is 9.00, true is returned.

If the price is 9.90, return fasle.

> Greater Price> 9.80

If the price is 9.90, true is returned.

If the price is 9.80, return fasle.

> = Greater than or equal Price >=9.80

If the price is 9.90, true is returned.

If the price is 9.70, return fasle.

Or 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 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 Calculate the remainder of the Division 5 mod 2 1

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.