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文档数据的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注创新互联行业资讯频道,感谢各位的阅读!
网页标题:XPath中怎么提取xml文档数据
网页路径:https://www.cdcxhl.com/article34/gssese.html
成都网站建设公司_创新互联,为您提供App开发、网站导航、网站设计、网站设计公司、网页设计公司、外贸网站建设
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联