当前位置:网站首页>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]");
边栏推荐
- 单调栈-84. 柱状图中最大的矩形
- Image processing 8-cnn image classification
- Display terrain database on osgearth ball
- 【Rust 笔记】10-操作符重载
- Pit & ADB wireless debugging of vivo real machine debugging
- Easy touch plug-in
- 【Rust笔记】05-错误处理
- Markdown learning
- Unity editor expansion - scrolling list
- Creation and content of mapnode -- osgearth rendering engine series (2)
猜你喜欢

Vscode, idea, VIM development tool shortcut keys

梯度下降法求解BP神经网络的简单Demo
![[concurrent programming] consistency hash](/img/5e/3d0a52caa8ca489a6e6267274bbb39.jpg)
[concurrent programming] consistency hash

Redis data structure

Some understandings of 3dfiles

Mall management system of database application technology course design

Detailed explanation of all transfer function (activation function) formulas of MATLAB neural network

【云原生】微服务之Feign的介绍与使用

Gradle's method of dynamically modifying APK package name

Unity interactive water ripple post-treatment
随机推荐
Unity multi open script
Unity editor expansion - controls, layouts
Explain sizeof, strlen, pointer, array and other combination questions in detail
Unity Editor Extension - event handling
[rust notes] 05 error handling
Mysql容器化(1)Docker安装MySQL
Unity Editor Extension - drag and drop
ArrayList
Display terrain database on osgearth ball
[concurrent programming] working mechanism and type of thread pool
数据分析练习题
Pit & ADB wireless debugging of vivo real machine debugging
Creation and content of mapnode -- osgearth rendering engine series (2)
Golang 时间格式整理
Markdown directory generation
Kwai 20200412 recruitment
Jupyter remote server configuration and server startup
Conversion between string and int types in golang
KunlunBase MeetUP 等您来!
Intersectionpicker in osgearth