如何使用XPath提取xml文檔數(shù)據(jù)
本文實(shí)例為大家分享了XPath提取xml文檔數(shù)據(jù)具體代碼,供大家參考,具體內(nèi)容如下
import java.util.List;
import org.dom4j.Document;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;
import org.junit.Test;
/*
* 使用XPath查找xml文檔數(shù)據(jù)
*
*/
public class DemoXPath {
@Test
//輸出book.xml中所有price元素節(jié)點(diǎn)的文本值
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é)點(diǎn)的文本值
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é)點(diǎn)的文本值
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的對應(yīng)的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>
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
使用Java 8 Lambda表達(dá)式將實(shí)體映射到DTO的操作
這篇文章主要介紹了使用Java 8 Lambda表達(dá)式將實(shí)體映射到DTO的操作,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-08-08
java利用pdfbox+poi往pdf插入數(shù)據(jù)
這篇文章主要給大家介紹了關(guān)于java利用pdfbox+poi如何往pdf插入數(shù)據(jù)的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2022-02-02
java后臺(tái)啟動(dòng)jar包的一些命令匯總
這篇文章主要介紹了java后臺(tái)啟動(dòng)jar包的一些命令匯總,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2023-03-03
詳解Java正則表達(dá)式中Pattern類和Matcher類
java.util.regex是一個(gè)用正則表達(dá)式所訂制的模式來對字符串進(jìn)行匹配工作的類庫包。包括兩個(gè)類Pattern和Matcher Pattern,Pattern是一個(gè)正則表達(dá)式經(jīng)編譯后的表現(xiàn)模式。Matcher對象是一個(gè)狀態(tài)機(jī)器,它依據(jù)Pattern對象做為匹配模式對字符串展開匹配檢查。2016-12-12
Netty中ChannelPoolHandler調(diào)用處理程序詳解
這篇文章主要介紹了Netty中ChannelPoolHandler調(diào)用處理程序詳解,Netty 是基于 Java NIO 的異步事件驅(qū)動(dòng)的網(wǎng)絡(luò)應(yīng)用框架,使用 Netty 可以快速開發(fā)網(wǎng)絡(luò)應(yīng)用,Netty 提供了高層次的抽象來簡化 TCP 和 UDP 服務(wù)器的編程,但是你仍然可以使用底層的 API,需要的朋友可以參考下2023-11-11
SpringBoot創(chuàng)建并簡單使用的實(shí)現(xiàn)
這篇文章主要介紹了SpringBoot創(chuàng)建并簡單使用的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10
Windows下安裝ElasticSearch的方法(圖文)
這篇文章主要介紹了Windows下安裝ElasticSearch的方法(圖文),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-01-01

