当前位置:网站首页>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 multi open script
- Transmit pictures with Base64 encoding
- Analysis of Alibaba canal principle
- Final review of Database Principles
- Redux - learning notes
- Intersectionpicker in osgearth
- JS ternary operator - learning notes (with cases)
- Swagger document configuration
- Mysql容器化(1)Docker安装MySQL
- Cloudcompare learning (1) - cloudcompare compilation and common plug-in implementation
猜你喜欢

OpenGL learning notes

UE4 source code reading_ Bone model and animation system_ Animation process

Unity Editor Extension - drag and drop

GIS实战应用案例100篇(七十八)-多规合一数据库设计及数据入库

Some understandings of 3dfiles

Servlet的生命周期

Message queue for interprocess communication

Binary to decimal, decimal to binary

基于SSM的校园失物招领平台,源码,数据库脚本,项目导入运行视频教程,论文撰写教程

Visual Studio (VS) shortcut keys
随机推荐
Eating fruit
matlab神經網絡所有傳遞函數(激活函數)公式詳解
Markdown directory generation
【Rust 笔记】08-枚举与模式
Redux - learning notes
Unity editor expansion - draw lines
【Rust 笔记】07-结构体
【Rust 笔记】09-特型与泛型
Explain sizeof, strlen, pointer, array and other combination questions in detail
C language student management system based on linked list, super detailed
图像处理8-CNN图像分类
【Rust 笔记】10-操作符重载
Unity4.3.1 engine source code compilation process
【Rust笔记】06-包和模块
Find the intersection of line segments
[audio and video] ijkplayer error code
Cesium for unreal quick start - simple scenario configuration
Unity Editor Extension - event handling
php-fpm软件的安装+openresty高速缓存搭建
Animation_ IK overview