如何使用XPath提取xml文檔數(shù)據
更新時間:2019年08月30日 10:52:14 作者:xiaomin_____
這篇文章主要介紹了如何使用XPath提取xml文檔數(shù)據,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了XPath提取xml文檔數(shù)據具體代碼,供大家參考,具體內容如下
import java.util.List;
import org.dom4j.Document;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;
import org.junit.Test;
/*
* 使用XPath查找xml文檔數(shù)據
*
*/
public class DemoXPath {
@Test
//輸出book.xml中所有price元素節(jié)點的文本值
public void test1() throws Exception {
SAXReader reader = new SAXReader();
Document document = reader.read("src/main/java/book.xml");
List<? extends Node> selectNodes = document.selectNodes("http://price");
for(Node node : selectNodes) {
String text = node.getText();
System.out.println(text);
}
}
@Test
//輸出book.xml中第二本書的price元素節(jié)點的文本值
public void test2() throws Exception {
SAXReader reader = new SAXReader();
Document document = reader.read("src/main/java/book.xml");
Node selectSingleNode = document.selectSingleNode("/bookshelf/book[2]/price");
String text = selectSingleNode.getText();
System.out.println(text);
}
@Test
//輸出book.xml中第二本書和第三本書的author元素節(jié)點的文本值
public void test3() throws Exception {
SAXReader reader = new SAXReader();
Document document = reader.read("src/main/java/book.xml");
List<? extends Node> selectSingleNode = document.selectNodes("/bookshelf/book[position()>1]/author");
for (Node node : selectSingleNode) {
String text = node.getText();
System.out.println(text);
}
}
@Test
//輸出book.xml中含有屬性id的所有name的文本值
public void test4() throws Exception {
SAXReader reader = new SAXReader();
Document document = reader.read("src/main/java/book.xml");
List<? extends Node> selectSingleNode = document.selectNodes("http://name[@id]");
for (Node node : selectSingleNode) {
String text = node.getText();
System.out.println(text);
}
}
@Test
//輸出book.xml中含有屬性id="1111"的name的文本值
public void test5() throws Exception {
SAXReader reader = new SAXReader();
Document document = reader.read("src/main/java/book.xml");
Node selectSingleNode = document.selectSingleNode("http://name[@id=\"1111\"]");
String text = selectSingleNode.getText();
System.out.println(text);
}
@Test
//輸出book.xml中含有屬性id="1112"的book的author的文本值
public void test6() throws Exception {
SAXReader reader = new SAXReader();
Document document = reader.read("src/main/java/book.xml");
Node selectSingleNode = document.selectSingleNode("http://book[name[@id=\"1112\"]]/author");
String text = selectSingleNode.getText();
System.out.println(text);
}
@Test
//輸出book.xml中第一本book的id的屬性值
public void test7() throws Exception {
SAXReader reader = new SAXReader();
Document document = reader.read("src/main/java/book.xml");
Node selectSingleNode = document.selectSingleNode("http://book[1]/name");
String text = selectSingleNode.valueOf("attribute::id");//獲取id屬性
System.out.println(text);
}
@Test
//輸出book.xml中book的name的id的屬性值為1112的對應的sn的屬性值
public void test8() throws Exception {
SAXReader reader = new SAXReader();
Document document = reader.read("src/main/java/book.xml");
List<? extends Node> selectNodes = document.selectNodes("http://book/name");
for (Node node : selectNodes) {
if(node.valueOf("attribute::id").equals("1112")) {
System.out.println(node.valueOf("attribute::sn"));
}
}
}
}
<dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency> <dependency> <groupId>dom4j</groupId> <artifactId>dom4j</artifactId> </dependency> <dependency> <groupId>jaxen</groupId> <artifactId>jaxen</artifactId> <version>1.1.6</version> </dependency>
<?xml version="1.0" encoding="utf-8"?> <bookshelf> <book> <name id="1111" sn="sdd8">Tomorrow</name> <author>Hiskell</author> <price>$40</price> </book> <book> <name id="1112" sn="sdd9">Goodbye to You</name> <author>Giddle</author> <price>$25</price> </book> <book> <name id="1113" sn="sdd0">Sea and Old</name> <author>Heminw</author> <price>$28</price> </book> </bookshelf>
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
Spring?component-scan?XML配置與@ComponentScan注解配置
這篇文章主要介紹了Spring?component-scan?XML配置與@ComponentScan注解配置,文章圍繞主題展開詳細的內容介紹,具有一定的參考價值,需要的小伙伴可以參考一下2022-09-09
jmeter添加自定義擴展函數(shù)之圖片base64編碼示例詳解
這篇文章主要介紹了jmeter添加自定義擴展函數(shù)之圖片base64編碼,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-01-01

