Xpath path expression and code display

What is Xpath path expression

  • Xpath path expression is a language for querying data in XML documents
  • Mastering XPath can greatly improve the development efficiency when extracting data
  • Learning the essence of XPath is to master the skills of using various forms of expressions

Xpath basic expressions

  • The most commonly used basic expressions

Examples are as follows:

 

  • Xpath predicate expression

Other selection conditions are added to the basic expression:

Jaxen

Jaxen is an open source XPath library written in java. It is suitable for many different object models, including DOM, XOM, dom4j and JDOM. The bottom layer of Dom4j relies on Jaxen to implement Xpath query, so when using Xpath for XML query, we need to install Jaxen first. Baidu searches for jaxen, downloads the jar package, and imports it into Reference Libraries.

The following is the use of dom4j for xpath operations. xml file to this blog xml documents in the

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 XPathTensor {
	public void xpath(String xpathExp) {
		String file = "。。。。。。。。。。。。。";
		SAXReader reader = new SAXReader();
		try {
			Document document = reader.read(file);
			List<Node> nodes = document.selectNodes(xpathExp);
			for (Node node : nodes) {
				Element emp = (Element) node;
				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) {
		XPathTensor xt = new XPathTensor();
		xt.xpath("/hr/employee");
		xt.xpath("//employee");
		xt.xpath("//employee[salary<8000]");

	}

}

 

Guess you like

Origin blog.csdn.net/qq_41459262/article/details/110765574
Recommended