当前位置:网站首页>Query XML documents with XPath
Query XML documents with XPath
2022-07-03 08:45:00 【The corner of fufu】
One 、XPath Path expression
XPath The path expression is XML The language in which data is queried in the document
expression | describe |
---|---|
nodename | Select all children of this node |
/ | Select from root node |
// | Select the node of the document from the current node that matches the selection , Regardless of its location |
. | Select the current node |
… | Select the parent of the current node |
@ | Select Properties |
Two 、jaxen
Jaxen It's a Java Compiling XPath library , It can adapt to many different object models , Such as :DOM,XOM,dom4j etc. .
Dom4j The underlying dependence Jaxen Realization Xpath Inquire about
3、 ... and 、 Program realization
xml page
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hr SYSTEM "hr.dtd"><!-- Human resource management system -->
<hr>
<employee no="3301">
<name> Zhang San </name>
<age>31</age>
<salary>3500</salary>
<department>
<dname> The administration department </dname>
<address>xx building -B103</address>
</department>
</employee>
<employee no="3302">
<name> Zhang Si </name>
<age>32</age>
<salary>4500</salary>
<department>
<dname> The administration department </dname>
<address>xx building -B103</address>
</department>
</employee>
<employee no="3303">
<name> Zhang Wu </name>
<age>33</age>
<salary>5200</salary>
<department>
<dname> The administration department </dname>
<address>xx building -B103</address>
</department>
</employee>
<employee no="3304">
<name> Zhang Liu </name>
<age>34</age>
<salary>5500</salary>
<department>
<dname> The administration department </dname>
<address>xx building -B103</address>
</department>
</employee>
<employee no="3305">
<name> Li Si </name>
<age>29</age>
<salary>8000</salary>
<department>
<dname> Technology Department </dname>
<address>xx building -B104</address>
</department>
</employee>
<employee no="3306">
<name> Li Wu </name>
<age>26</age>
<salary>7000</salary>
<department>
<dname> Technology Department </dname>
<address>xx building -B104</address>
</department>
</employee>
<employee no="3307">
<name> Wang Wu </name>
<age>24</age>
<salary>3600</salary>
<department>
<dname> The personnel department </dname>
<address>XX building -B205</address>
</department>
</employee>
</hr>
java page
package com.imooc.dom4j;
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 XPathTester {
public void xpath(String xpathExp) {
String file = "d:/workplace/xml/src/hr.xml";
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.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) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
XPathTester tester = new XPathTester();
// tester.xpath("/hr/employee");
// tester.xpath("//employee");
// tester.xpath("//employee[salary<4000]");
// tester.xpath("//employee[name=' Li Si ']");
// tester.xpath("//employee[@no=3305]");
// tester.xpath("//employee[1]");
// tester.xpath("//employee[last()]");
// tester.xpath("//employee[position()<4]");
tester.xpath("//employee[3] | //employee[5]");
}
}
Four 、 Query output
Overall query
tester.xpath("//employee");
Wages less than 4000 The employees'
tester.xpath("//employee[salary<4000]");
lookup " Li Si " This employee
tester.xpath("//employee[name=' Li Si ']");
The number is “3305” The employees'
tester.xpath("//employee[@no=3305]");
The first employee
tester.xpath("//employee[1]");
The last employee
tester.xpath("//employee[last()]");
The first three employees
tester.xpath("//employee[position()<4]");
The third and fifth employees
tester.xpath("//employee[3] | //employee[5]");
边栏推荐
- Unity notes 1
- 基于SSM的校园失物招领平台,源码,数据库脚本,项目导入运行视频教程,论文撰写教程
- 请求参数的发送和接收
- UE4 source code reading_ Bone model and animation system_ Animation process
- [audio and video] ijkplayer error code
- Downward compatibility and upward compatibility
- matlab神经网络所有传递函数(激活函数)公式详解
- Alibaba canal actual combat
- Explain sizeof, strlen, pointer, array and other combination questions in detail
- [concurrent programming] thread foundation and sharing between threads
猜你喜欢
[concurrent programming] Table hopping and blocking queue
Chocolate installation
数据库原理期末复习
ES6 promise learning notes
Thymeleaf 404 reports an error: there was unexpected error (type=not found, status=404)
Redux - learning notes
Notes on understanding applets 2022/7/3
Vscode, idea, VIM development tool shortcut keys
Mxone Pro adaptive 2.0 film and television template watermelon video theme apple cmsv10 template
[concurrent programming] working mechanism and type of thread pool
随机推荐
matlab神经网络所有传递函数(激活函数)公式详解
GIS实战应用案例100篇(七十八)-多规合一数据库设计及数据入库
详解sizeof、strlen、指针和数组等组合题
请求参数的发送和接收
【Rust 笔记】13-迭代器(上)
JS ternary operator - learning notes (with cases)
Kwai 20200412 recruitment
100 GIS practical application cases (78) - Multi compliance database design and data warehousing
Alibaba canaladmin deployment and canal cluster Ha Construction
Mysql容器化(1)Docker安装MySQL
LinkedList set
producer consumer problem
【Rust笔记】06-包和模块
Data analysis exercises
Intersectionpicker in osgearth
Find the intersection of line segments
Servlet的生命周期
[updating] wechat applet learning notes_ three
简易入手《SOM神经网络》的本质与原理
Cesium service deployment, and import and display local 3dfiles data