Introduction to XPath
XPath is the XML Path Language (XML Path Language), which is an XML document is used to determine the position of a part of the language.
XPath-based XML tree structure, providing the ability to find a node in the data tree. XPath mind initially proposed that it as a general-purpose, between XPointer and XSL grammar model between. But XPath soon be employed to developers as a small query language .
The basic XPath expression
expression | description |
---|---|
nodename | Select all the child nodes of this node. |
/ | Choose from the root node. |
// | Select the document matches the selected node from the current node, regardless of their location. |
. | Select the current node. |
.. | Select the parent of the current node. |
@ | Select Properties. |
The basic XPath expression case
Path expression | result |
---|---|
bookstore | Selects all child nodes of the bookstore element. |
/bookstore | Select the root element bookstore. Note: If the path starts with a forward slash (/), then this path is always representative of the absolute path to an element! |
bookstore/book | Select the sub-elements belonging to the bookstore of the book all the elements. |
//book | Select all book sub-elements, regardless of their position in the document. |
bookstore//book | Select all book elements that belong to the descendants of the bookstore element, and no matter what position they are located below the bookstore. |
// @ lang | Select all of the property named lang. |
XPath predicate expression
Path expression | result |
---|---|
/bookstore/book[1] | A first element selected book child elements belonging bookstore |
/bookstore/book[late()] | The last book element selected sub-elements belonging to bookstore |
/bookstore/book[late()-1] | Select the sub-elements belonging to the reciprocal of the second bookstore book element |
/bookstore/book[position()<3] | The first two elements selected book belonging to the bookstore element subelements |
//title[@lang] | Select all of lang has a property named title element |
// title [@ lang = 'What'] | Select all the title elements, and these elements have a lang attribute value eng |
/bookstore/book[price>35.00] | Select all the book elements bookstore element, and wherein the price element value must be greater than 35.00 |
/bookstore/book[price>35.00]/title | Select book element bookstore element of all the title elements, and wherein the price element value must be greater than 35.00 |
Dom4j underlying rely Jaxen achieve XPath query
Jaxen Introduction
- Jaxen is written in Java open source XPath library. Here adapt to a variety of different object model, including DOM, XOM, dom4j and JDOM.
- Dom4j underlying rely Jaxen achieve XPath query
- Jaxen Download: jaxen.codehaus.org , because it is outside the network, it is generally from Ali cloud repository download
XPath Queries
import java.util.List;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;
public class XPathTestor {
public void xpath(String xpathExp){
String file = "E:/workspace/eclipse/HelloWorld/src/test2/hr.xml";
SAXReader reader = new SAXReader();
try {
Document document = reader.read(file);
List<Node> nodes = document.selectNodes(xpathExp);//解析xpath表达式
for(Node node : nodes){
Element emp = (Element)node;
System.out.println(emp.attributeValue("no"));
System.out.println(emp.elementText("name"));
System.out.println(emp.elementText("age"));
System.out.println(emp.elementText("salary"));
System.out.println("==============================");
}
} catch (DocumentException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
XPathTestor testor = new XPathTestor();
// testor.xpath("/hr/employee");//
// testor.xpath("//employee");
// testor.xpath("//employee[salary<4000]");
// testor.xpath("//employee[name='李铁柱']");
// testor.xpath("//employee[@no=3304]");
// testor.xpath("//employee[1]");
// testor.xpath("//employee[last()]");
//testor.xpath("//employee[position()<3]");
testor.xpath("//employee[3] | //employee[8]");
}
}
to sum up
When writing XML applications, query priority when it comes to using XPath expressions.
references:
https://baike.baidu.com/item/XPath/5574064