-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathselector.java
More file actions
80 lines (71 loc) · 1.76 KB
/
selector.java
File metadata and controls
80 lines (71 loc) · 1.76 KB
1
2
3
4
5
6
7
8
9
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import java.io.File;
import java.io.IOException;
/**
*选择器查询
*/
public class selector {
public static void main(String[] args) throws IOException {
//1.获取student.xml的path
String path = selector.class.getClassLoader().getResource("student.xml").getPath();
//2.获取Document对象
Document document = Jsoup.parse(new File(path), "UTF-8");
//3.查询name标签
/*
div{
}
*/
Elements elements = document.select("name");
System.out.println(elements);
System.out.println("-------------");
//4.查询id值为itcast的元素
Elements elements1 = document.select("#cpu");
System.out.println(elements1);
System.out.println("----------------");
//5.获取student标签并且number属性值为02的age子标签
//5.1.获取student标签并且number属性值为02
Elements elements2 = document.select("student[number=\"02\"]");
System.out.println(elements2);
System.out.println("-------------");
//5.2获取student标签并且number属性值为01的age子标签
Elements elements3 = document.select("student[number=\"01\"] > age");
System.out.println(elements3);
}
}
/**
* <name id="cpu">
* <cpu>
* cpu
* </cpu>
* <code>code</code>
* </name>
* <name>
* cpu_code
* </name>
* -------------
* <name id="cpu">
* <cpu>
* cpu
* </cpu>
* <code>code</code>
* </name>
* ----------------
* <student number="02">
* <name>
* cpu_code
* </name>
* <age>
* 29
* </age>
* <sex>
* 女
* </sex>
* </student>
* -------------
* <age>
* 20
* </age>
*
**/