快捷方式:
select:选择器
使用的方法:Element select(String CssQuery)
语法:参数selector类中定义语法
String path = JsoupDemo5.class.getClassLoader().getResource("student.xml").getPath(); Document document = Jsoup.parse(new File(path), "utf-8"); //查询name标签 Elements name = document.select("name"); System.out.println(name); //查询id值为example Elements select = document.select("#example"); System.out.println(select); Elements select1 = document.select("studnet[number=\"heima_0001\"]"); System.out.println(select1); Elements select2 = document.select("studnet[number=\"heima_0001\"] > age"); System.out.println(select2);
xml_解析_jsoup根据Xpath查询
Xpath:Xpath及为XMl路径语言,他是一种用来确定XmL(标准通用标记语言的子集)文档中某部分位置的语言
使用Jsoup的Xpath需求额导入jar包
查询w3school参考手册,使用xapth的语法完成查询
public static void main(String[] args) throws IOException { String path = JsoupDemo6.class.getClassLoader().getResource("student.xml").getPath(); Document document = Jsoup.parse(new File(path), "utf-8"); JXDocument jxDocument = new JXDocument(document); List<JXNode> jxNodes = jxDocument.selN("//student"); for (JXNode jxNode: jxNodes) { System.out.println(jxNode); } List<JXNode> jxNodes2 = jxDocument.selN("//student/name"); for (JXNode jxNode: jxNodes2) { System.out.println(jxNode); } List<JXNode> jxNodes3= jxDocument.selN("//student/name[@id]"); for (JXNode jxNode: jxNodes3) { System.out.println(jxNode); } List<JXNode> jxNodes4 = jxDocument.selN("//student/name[@id='example']"); for (JXNode jxNode: jxNodes4) { System.out.println(jxNode);
浙公网安备 33010602011771号