// 使用XPath 获取name元素
public static void test1() throws Exception{
//1.创建解析器
SAXReader saxReader = new SAXReader();
//2.创建Document
Document document = saxReader.read(PATH);
//3.使用XPATH
Node name = document.selectSingleNode("//p1[@id='abc']/name");
//4.获取name的文本
System.out.println(name.getText());
}
//使用XPath 获取所有的name元素
public static void AllName() throws Exception{
//1.创建构造器
SAXReader saxReader = new SAXReader();
//2.获取Document
Document document = saxReader.read(PATH);
//3.获取所有的name
List<Node> list = document.selectNodes("//name");
//4.遍历获取所有值
for (Node node : list){
System.out.println(node.getText());
}
}
网友评论