当前位置:网站首页>XPath实现XML文档的查询
XPath实现XML文档的查询
2022-07-03 08:35:00 【緈福的街口】
一、XPath路径表达式
XPath路径表达式是XML文档中查询数据的语言
| 表达式 | 描述 |
|---|---|
| nodename | 选取此节点的所有子节点 |
| / | 从根节点选取 |
| // | 从匹配选择的当前节点选择文档的节点,不考虑其位置 |
| . | 选取当前节点 |
| … | 选取当前节点的父节点 |
| @ | 选取属性 |
二、jaxen
Jaxen是一个Java编写的XPath库,可以适应多种不同的对象模型,如:DOM,XOM,dom4j等。
Dom4j底层依赖Jaxen实现Xpath查询
三、程序实现
xml页面
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hr SYSTEM "hr.dtd"><!-- 人力资源管理系统 -->
<hr>
<employee no="3301">
<name>张三</name>
<age>31</age>
<salary>3500</salary>
<department>
<dname>行政部</dname>
<address>xx大厦-B103</address>
</department>
</employee>
<employee no="3302">
<name>张四</name>
<age>32</age>
<salary>4500</salary>
<department>
<dname>行政部</dname>
<address>xx大厦-B103</address>
</department>
</employee>
<employee no="3303">
<name>张五</name>
<age>33</age>
<salary>5200</salary>
<department>
<dname>行政部</dname>
<address>xx大厦-B103</address>
</department>
</employee>
<employee no="3304">
<name>张六</name>
<age>34</age>
<salary>5500</salary>
<department>
<dname>行政部</dname>
<address>xx大厦-B103</address>
</department>
</employee>
<employee no="3305">
<name>李四</name>
<age>29</age>
<salary>8000</salary>
<department>
<dname>技术部</dname>
<address>xx大厦-B104</address>
</department>
</employee>
<employee no="3306">
<name>李五</name>
<age>26</age>
<salary>7000</salary>
<department>
<dname>技术部</dname>
<address>xx大厦-B104</address>
</department>
</employee>
<employee no="3307">
<name>王五</name>
<age>24</age>
<salary>3600</salary>
<department>
<dname>人事部</dname>
<address>XX大厦-B205</address>
</department>
</employee>
</hr>
java页面
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='李四']");
// tester.xpath("//employee[@no=3305]");
// tester.xpath("//employee[1]");
// tester.xpath("//employee[last()]");
// tester.xpath("//employee[position()<4]");
tester.xpath("//employee[3] | //employee[5]");
}
}
四、查询输出
总体查询
tester.xpath("//employee");
工资小于4000的员工
tester.xpath("//employee[salary<4000]");
查找"李四"这个员工
tester.xpath("//employee[name='李四']");
编号为“3305”的员工
tester.xpath("//employee[@no=3305]");
第一个员工
tester.xpath("//employee[1]");
最后一个员工
tester.xpath("//employee[last()]");
前三个员工
tester.xpath("//employee[position()<4]");
第三个和第五个员工
tester.xpath("//employee[3] | //employee[5]");
边栏推荐
- 100 GIS practical application cases (78) - Multi compliance database design and data warehousing
- [cloud native] introduction and use of feign of microservices
- UE4 source code reading_ Mobile synchronization
- Solution détaillée de toutes les formules de fonction de transfert (fonction d'activation) du réseau neuronal MATLAB
- 基于SSM的校园失物招领平台,源码,数据库脚本,项目导入运行视频教程,论文撰写教程
- [redis] redis persistent RDB vs AOF (source code)
- jupyter远程服务器配置以及服务器开机自启
- Cesium service deployment, and import and display local 3dfiles data
- Animation_ IK overview
- Golang time format sorting
猜你喜欢
随机推荐
[concurrent programming] Table hopping and blocking queue
OpenGL learning notes
Animation_ IK overview
Base64编码简介
Pit & ADB wireless debugging of vivo real machine debugging
【Rust 笔记】13-迭代器(上)
Eating fruit
GIS实战应用案例100篇(七十八)-多规合一数据库设计及数据入库
Graphics_ Learnopongl learning notes
LinkedList set
Jupyter remote server configuration and server startup
Osgearth topographic shading map drawing
Visual Studio (VS) shortcut keys
MySQL 8
Base64和Base64URL
【Rust 笔记】10-操作符重载
php-fpm软件的安装+openresty高速缓存搭建
redis集群系列四
了解小程序的笔记 2022/7/3
Dotween plug-in









