当前位置:网站首页>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]");
边栏推荐
- Base64编码简介
- Image processing 8-cnn image classification
- [concurrent programming] thread foundation and sharing between threads
- 2021-10-19
- C course design employee information management system
- Base64 and base64url
- Downward compatibility and upward compatibility
- Unity multi open script
- Markdown learning
- 【Rust 笔记】10-操作符重载
猜你喜欢

Student educational administration management system of C # curriculum design

matlab神经网络所有传递函数(激活函数)公式详解

Vscode, idea, VIM development tool shortcut keys

C course design employee information management system

Data analysis exercises

Introduction to hexadecimal coding

Thymeleaf 404 reports an error: there was unexpected error (type=not found, status=404)

Installation of PHP FPM software +openresty cache construction

Markdown learning
![[concurrent programming] concurrent tool class of thread](/img/16/2b4d2b3528b138304a1a3918773ecf.jpg)
[concurrent programming] concurrent tool class of thread
随机推荐
ArrayList
【更新中】微信小程序学习笔记_3
Minimap plug-in
Osgearth target selection
Initial unity
animation
基于SSM的校园失物招领平台,源码,数据库脚本,项目导入运行视频教程,论文撰写教程
单调栈-503. 下一个更大元素 II
[RPC] RPC remote procedure call
UE4 source code reading_ Bone model and animation system_ Animation compression
[concurrent programming] concurrent tool class of thread
[rust notes] 02 ownership
Gradle's method of dynamically modifying APK package name
Unity learning notes
Sequence of map implementation classes
Ue5 opencv plug-in use
数据分析练习题
Simple demo of solving BP neural network by gradient descent method
UE4 source code reading_ Mobile synchronization
LinkedList set