当前位置:网站首页>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]");
边栏推荐
- Cesium service deployment, and import and display local 3dfiles data
- Annotations simplify configuration and loading at startup
- matlab神经网络所有传递函数(激活函数)公式详解
- Transmit pictures with Base64 encoding
- Kwai 20200412 recruitment
- Find the intersection of line segments
- Constraintlayout's constraintset dynamically modifies constraints
- Alibaba canal actual combat
- Talking about: is the HashSet set ordered or disordered /hashset set unique, why can we store elements with the same content
- 详解sizeof、strlen、指针和数组等组合题
猜你喜欢

Alibaba canal actual combat
![[concurrent programming] working mechanism and type of thread pool](/img/51/d21428a7c95c0a5177e8198742e78c.jpg)
[concurrent programming] working mechanism and type of thread pool

Kwai 20200412 recruitment

Constraintlayout's constraintset dynamically modifies constraints

XPath实现XML文档的查询

【Rust笔记】02-所有权

简易入手《SOM神经网络》的本质与原理

注解简化配置与启动时加载

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

Visual Studio (VS) shortcut keys
随机推荐
【Rust 笔记】10-操作符重载
Detailed explanation of all transfer function (activation function) formulas of MATLAB neural network
UE4 source code reading_ Bone model and animation system_ Animation process
VIM learning notes from introduction to silk skating
Osganimation library parsing
【Rust 笔记】11-实用特型
Life cycle of Servlet
[concurrent programming] explicit lock and AQS
Unity editor expansion - draw lines
Creation and content of mapnode -- osgearth rendering engine series (2)
Osgearth starry background
Monotonic stack -503 Next bigger Element II
Explain sizeof, strlen, pointer, array and other combination questions in detail
Creation of osgearth earth files to the earth ------ osgearth rendering engine series (1)
URL backup 1
producer consumer problem
LinkedList set
[rust notes] 06 package and module
Cloudcompare learning (1) - cloudcompare compilation and common plug-in implementation
Osgearth topographic shading map drawing