您好,登錄后才能下訂單哦!
這篇文章將為大家詳細講解有關使用XPath提取xml文檔數據的案例,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。
XPath提取xml文檔數據具體內容如下
import java.util.List; import org.dom4j.Document; import org.dom4j.Node; import org.dom4j.io.SAXReader; import org.junit.Test; /* * 使用XPath查找xml文檔數據 * */ public class DemoXPath { @Test //輸出book.xml中所有price元素節點的文本值 public void test1() throws Exception { SAXReader reader = new SAXReader(); Document document = reader.read("src/main/java/book.xml"); List<? extends Node> selectNodes = document.selectNodes("//price"); for(Node node : selectNodes) { String text = node.getText(); System.out.println(text); } } @Test //輸出book.xml中第二本書的price元素節點的文本值 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元素節點的文本值 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("//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("//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("//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("//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("//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>
關于“使用XPath提取xml文檔數據的案例”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,使各位可以學到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。