JavaWeb12.5【XML:快捷查询方式-selector选择器和XPath】
https://www.w3school.com.cn/xpath/index.asp
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <students> 3 <student number="itcast_0001"> 4 <!--<name id="itcast">tom</name>--> 5 <name id="itcast"> 6 <xing>张</xing> 7 <ming>三</ming> 8 </name> 9 <age>9999</age> 10 <sex>male</sex> 11 </student> 12 <student number="itcast_0002"> 13 <name>sam</name> 14 <age>20</age> 15 <sex>female</sex> 16 </student> 17 </students>
1 package com.haifei.jsoup; 2 3 import org.jsoup.Jsoup; 4 import org.jsoup.nodes.Document; 5 import org.jsoup.select.Elements; 6 7 import java.io.File; 8 import java.io.IOException; 9 10 /** 11 * 快捷查询方式之selector选择器 12 */ 13 public class JsoupDemo5 { 14 public static void main(String[] args) throws IOException { 15 String path = JsoupDemo1.class.getClassLoader().getResource("student.xml").getPath(); 16 Document document = Jsoup.parse(new File(path), "utf-8"); 17 18 //1 查询name标签 19 Elements elements = document.select("name"); 20 // System.out.println(elements); 21 /* 22 <name id="itcast"> 23 <xing> 24 张 25 </xing> 26 <ming> 27 三 28 </ming> 29 </name> 30 <name> 31 sam 32 </name> 33 */ 34 35 //2 查询id值为itcast的元素 36 Elements elements1 = document.select("#itcast"); 37 // System.out.println(elements1); 38 /* 39 <name id="itcast"> 40 <xing> 41 张 42 </xing> 43 <ming> 44 三 45 </ming> 46 </name> 47 */ 48 49 //3 获取number属性值为itcast_0001的student标签下的子标签age 50 /*Elements elements2 = document.select("student[number='itcast_0001']"); 51 System.out.println(elements2);*/ 52 Elements elements3 = document.select("student[number='itcast_0001'] > age"); 53 System.out.println(elements3); 54 /* 55 <age> 56 9999 57 </age> 58 */ 59 60 //4 更多查询用法参考API中Selector类中定义的语法 61 } 62 }
1 package com.haifei.jsoup; 2 3 import cn.wanghaomiao.xpath.exception.XpathSyntaxErrorException; 4 import cn.wanghaomiao.xpath.model.JXDocument; 5 import cn.wanghaomiao.xpath.model.JXNode; 6 import org.jsoup.Jsoup; 7 import org.jsoup.nodes.Document; 8 9 import java.io.File; 10 import java.io.IOException; 11 import java.util.List; 12 13 /** 14 * 快捷查询方式之XPath 15 * 导入jsoup包 16 * 导入jsoupXpath包 17 */ 18 public class JsoupDemo6 { 19 public static void main(String[] args) throws IOException, XpathSyntaxErrorException { 20 //1.获取student.xml的path 21 String path = JsoupDemo1.class.getClassLoader().getResource("student.xml").getPath(); 22 //2.获取Document对象 23 Document document = Jsoup.parse(new File(path), "utf-8"); 24 25 //3.根据Document对象,创建JXDocument对象 26 JXDocument jxDocument = new JXDocument(document); 27 28 //4. 结合XPath语法进行查询 29 //4.1 查询所有student标签 30 List<JXNode> jxNodes = jxDocument.selN("//student"); 31 for (JXNode jxNode: jxNodes){ 32 // System.out.println(jxNode); 33 // jxNode.getElement(); 34 } 35 //4.2 查询所有student标签下的子标签name 36 List<JXNode> jxNodes2 = jxDocument.selN("//student/name"); 37 for (JXNode jxNode:jxNodes2){ 38 System.out.println(jxNode); 39 } 40 System.out.println(); 41 /* 42 <name id="itcast"> 43 <xing> 44 张 45 </xing> 46 <ming> 47 三 48 </ming> 49 </name> 50 <name> 51 sam 52 </name> 53 */ 54 55 //4.3 查询所有student标签下带有id属性的子标签name 56 List<JXNode> jxNodes3 = jxDocument.selN("//student/name[@id]"); 57 for (JXNode jxNode:jxNodes3){ 58 System.out.println(jxNode); 59 } 60 System.out.println(); 61 /* 62 <name id="itcast"> 63 <xing> 64 张 65 </xing> 66 <ming> 67 三 68 </ming> 69 </name> 70 */ 71 72 //4.4 查询所有student标签下带有id属性的子标签name,且id属性值为itcast 73 List<JXNode> jxNodes4 = jxDocument.selN("//student/name[@id='itcast']"); 74 for (JXNode jxNode:jxNodes4){ 75 System.out.println(jxNode); 76 } 77 System.out.println(); 78 /* 79 <name id="itcast"> 80 <xing> 81 张 82 </xing> 83 <ming> 84 三 85 </ming> 86 </name> 87 */ 88 } 89 }