¸üÐÂʱ¼ä:2019Äê01ÔÂ10ÈÕ13ʱ56·Ö À´Ô´:ÀÖÓã²¥¿Í ä¯ÀÀ´ÎÊý:
|
01
02
03
04
05
06
07
08
09
10
11
12
13
|
//2.1»ñÈ¡student.xmlµÄpath String path = JsoupDemo1.class.getClassLoader().getResource("student.xml").getPath(); //2.2½âÎöxmlÎĵµ£¬¼ÓÔØÎĵµ½øÄڴ棬»ñÈ¡domÊ÷--->Document Document document = Jsoup.parse(new File(path), "utf-8"); //3.»ñÈ¡ÔªËØ¶ÔÏó Element Elements elements = document.getElementsByTag("name"); System.out.println(elements.size()); //3.1»ñÈ¡µÚÒ»¸önameµÄElement¶ÔÏó Element element = elements.get(0); //3.2»ñÈ¡Êý¾Ý String name = element.text(); System.out.println(name); |
|
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
//1.»ñÈ¡student.xmlµÄpath String path = JsoupDemo6.class.getClassLoader().getResource("student.xml").getPath(); //2.»ñÈ¡Document¶ÔÏó Document document = Jsoup.parse(new File(path), "utf-8"); //3.¸ù¾Ýdocument¶ÔÏ󣬴´½¨JXDocument¶ÔÏó JXDocument jxDocument = new JXDocument(document); //4.½áºÏxpathÓï·¨²éѯ //4.1²éѯËùÓÐstudent±êÇ© List<JXNode> jxNodes = jxDocument.selN("//student"); for (JXNode jxNode : jxNodes) { System.out.println(jxNode); } System.out.println("--------------------"); //4.2²éѯËùÓÐstudent±êǩϵÄname±êÇ© List<JXNode> jxNodes2 = jxDocument.selN("//student/name"); for (JXNode jxNode : jxNodes2) { System.out.println(jxNode); } System.out.println("--------------------"); //4.3²éѯstudent±êǩϴøÓÐidÊôÐÔµÄname±êÇ© List<JXNode> jxNodes3 = jxDocument.selN("//student/name[@id]"); for (JXNode jxNode : jxNodes3) { System.out.println(jxNode); } System.out.println("--------------------"); //4.4²éѯstudent±êǩϴøÓÐidÊôÐÔµÄname±êÇ© ²¢ÇÒidÊôÐÔֵΪitcast List<JXNode> jxNodes4 = jxDocument.selN("//student/name[@id='itcast']"); for (JXNode jxNode : jxNodes4) { System.out.println(jxNode); } |
×÷ÕߣºÀÖÓã²¥¿ÍJavaEEÅàѵѧԺ
Ê×·¢£ºhttp://java.itcast.cn
±±¾©Ð£Çø