-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDocument_object.java
More file actions
92 lines (85 loc) · 1.85 KB
/
Document_object.java
File metadata and controls
92 lines (85 loc) · 1.85 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
81
82
83
84
85
86
87
88
89
90
91
92
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import java.io.File;
import java.io.IOException;
/**
* Document/Element对象功能
*/
public class Document_object {
public static void main(String[] args) throws IOException {
//1.获取student.xml的path
String path = Document_object.class.getClassLoader().getResource("student.xml").getPath();
//2.获取Document对象
Document document= Jsoup.parse(new File(path), "UTF-8");
//3.获取元素对象了。
//3.1获取所有student对象
Elements elements = document.getElementsByTag("student");
System.out.println(elements);
System.out.println("----------------------");
//3.2 获取属性名为id的元素对象们
Elements elements1 = document.getElementsByAttribute("id");
System.out.println(elements1);
System.out.println("------------------------");
//3.2获取 number属性值为02的元素对象
Elements elements2 = document.getElementsByAttributeValue("number", "02");
System.out.println(elements2);
System.out.println("------------------------");
Element element = document.getElementById("cpu");
System.out.println(element);
}
}
/*
<student number="01">
<name id="cpu">
<cpu>
cpu
</cpu>
<code>code</code>
</name>
<age>
20
</age>
<sex>
男
</sex>
</student>
<student number="02">
<name>
cpu_code
</name>
<age>
29
</age>
<sex>
女
</sex>
</student>
----------------------
<name id="cpu">
<cpu>
cpu
</cpu>
<code>code</code>
</name>
------------------------
<student number="02">
<name>
cpu_code
</name>
<age>
29
</age>
<sex>
女
</sex>
</student>
------------------------
<name id="cpu">
<cpu>
cpu
</cpu>
<code>code</code>
</name>
* */