app开发者平台在数字化时代的重要性与发展趋势解析
560
2023-07-24
如何使用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 http://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();
SbHebOxRwf 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"));
}
}
}
}
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~