当前位置:网站首页>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]");
边栏推荐
- Unity editor expansion - controls, layouts
- [updating] wechat applet learning notes_ three
- Graphics_ Games101/202 learning notes
- 【Rust 笔记】07-结构体
- Osgearth topographic shading map drawing
- P1596 [USACO10OCT]Lake Counting S
- Redis data structure
- 如何应对数仓资源不足导致的核心任务延迟
- Golang string segmentation, substitution and interception
- Some understandings of 3dfiles
猜你喜欢
Creation of osgearth earth files to the earth ------ osgearth rendering engine series (1)
Cloudcompare learning (1) - cloudcompare compilation and common plug-in implementation
Solution détaillée de toutes les formules de fonction de transfert (fonction d'activation) du réseau neuronal MATLAB
Base64和Base64URL
Minimap plug-in
Ue5 opencv plug-in use
Introduction to hexadecimal coding
[RPC] RPC remote procedure call
Introduction to Base64 coding
php-fpm软件的安装+openresty高速缓存搭建
随机推荐
數據庫應用技術課程設計之商城管理系統
Creation and content of mapnode -- osgearth rendering engine series (2)
使用base64编码传图片
php-fpm软件的安装+openresty高速缓存搭建
100 GIS practical application cases (78) - Multi compliance database design and data warehousing
[cloud native] introduction and use of feign of microservices
Unity editor expansion - the framework and context of unity imgui
swagger文档配置
Redis data structure
Constraintlayout's constraintset dynamically modifies constraints
MXone Pro自适应2.0影视模板西瓜视频主题苹果cmsV10模板
Cesium for unreal quick start - simple scenario configuration
Kunlunbase meetup is waiting for you!
了解小程序的笔记 2022/7/3
producer consumer problem
Sequence of map implementation classes
Osgearth target selection
Base64编码简介
Golang url的编码和解码
Unity editor expansion - the design idea of imgui