-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjsoup_object.java
More file actions
109 lines (103 loc) · 2.41 KB
/
jsoup_object.java
File metadata and controls
109 lines (103 loc) · 2.41 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import java.io.File;
import java.io.IOException;
import java.net.URL;
public class jsoup_object {
public static void main(String[] args) throws IOException {
//2.1获取student.xml的path
String path = jsoup_object.class.getClassLoader().getResource("student.xml").getPath();
//2.2解析xml文档,加载文档进内存,获取dom树--->Document
Document parse = Jsoup.parse(new File(path), "UTF-8");
System.out.println(parse);
//2.parse(String html):解析xml或html字符串
String str = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n" +
"\n" +
"<students>\n" +
"\t<student number=\"01\">\n" +
"\t\t<name>cpu_code</name>\n" +
"\t\t<age>22</age>\n" +
"\t\t<sex>女</sex>\n" +
"\t</student>\n" +
"\t<student number=\"02\">\n" +
"\t\t<name>code</name>\n" +
"\t\t<age>33</age>\n" +
"\t\t<sex>男</sex>\n" +
"\t</student>\n" +
"\n" +
"</students>";
Document parse1 = Jsoup.parse(str);
System.out.println(parse1);
//3.parse(URL url, int timeoutMillis):通过网络路径获取指定的html或xml的文档对象
/*
URL url = new URL("http://jw.hieu.edu.cn/jwglxt/xtgl/login_slogin.html"); //代表网络中的一个资源路径
Document document = Jsoup.parse(url, 5000);
System.out.println(document);
*/
}
}
/*
<!--?xml version="1.0" encoding="UTF-8" ?-->
<html>
<head></head>
<body>
<students>
<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>
</students>
</body>
</html>
<!--?xml version="1.0" encoding="UTF-8" ?-->
<html>
<head></head>
<body>
<students>
<student number="01">
<name>
cpu_code
</name>
<age>
22
</age>
<sex>
女
</sex>
</student>
<student number="02">
<name>
code
</name>
<age>
33
</age>
<sex>
男
</sex>
</student>
</students>
</body>
</html>
* */