如何使用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的對(duì)應(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>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java模仿微信實(shí)現(xiàn)零錢通簡(jiǎn)易功能(兩種版本)
本文主要介紹了使用Java開發(fā)零錢通項(xiàng)目, 模仿微信實(shí)現(xiàn)簡(jiǎn)易功能,可以完成收益入賬,消費(fèi),查看明細(xì),退出系統(tǒng)等功能。文中一共介紹了兩種實(shí)現(xiàn)方法,快來(lái)學(xué)習(xí)吧2021-12-12JAVA實(shí)現(xiàn)sm3加密簽名以及防止重復(fù)攻擊
這篇文章主要給大家介紹了關(guān)于JAVA實(shí)現(xiàn)sm3加密簽名以及防止重復(fù)攻擊的相關(guān)資料,SM3是簽名算法,和MD5一樣(對(duì)于應(yīng)用層來(lái)說(shuō)),SM4是對(duì)稱加密算法,和AES一樣(對(duì)于應(yīng)用層來(lái)說(shuō)),需要的朋友可以參考下2023-10-10java郵件發(fā)送簡(jiǎn)單實(shí)現(xiàn)代碼
這篇文章主要為大家詳細(xì)介紹了java郵件發(fā)送簡(jiǎn)單實(shí)現(xiàn)代碼,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-03-03Spring?component-scan?XML配置與@ComponentScan注解配置
這篇文章主要介紹了Spring?component-scan?XML配置與@ComponentScan注解配置,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-09-09jmeter添加自定義擴(kuò)展函數(shù)之圖片base64編碼示例詳解
這篇文章主要介紹了jmeter添加自定義擴(kuò)展函數(shù)之圖片base64編碼,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-01-01Java解決浮點(diǎn)數(shù)計(jì)算不精確問(wèn)題的方法詳解
在 Java 中,浮點(diǎn)數(shù)計(jì)算不精確問(wèn)題指的是使用浮點(diǎn)數(shù)進(jìn)行運(yùn)算時(shí),由于浮點(diǎn)數(shù)的內(nèi)部表示方式和十進(jìn)制數(shù)的表示方式存在差異,導(dǎo)致計(jì)算結(jié)果可能出現(xiàn)誤差,本文就給大家介紹一下Java如何解決浮點(diǎn)數(shù)計(jì)算不精確問(wèn)題,需要的朋友可以參考下2023-09-09