-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathWikiNodeExample.java
More file actions
79 lines (63 loc) · 1.91 KB
/
WikiNodeExample.java
File metadata and controls
79 lines (63 loc) · 1.91 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
package com.allendowney.thinkdast;
import java.io.IOException;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Deque;
import java.util.List;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.nodes.Node;
import org.jsoup.nodes.TextNode;
import org.jsoup.select.Elements;
public class WikiNodeExample {
public static void main(String[] args) throws IOException {
String url = "https://en.wikipedia.org/wiki/Java_(programming_language)";
// download and parse the document
Connection conn = Jsoup.connect(url);
Document doc = conn.get();
// select the content text and pull out the paragraphs.
Element content = doc.getElementById("mw-content-text");
// TODO: avoid selecting paragraphs from sidebars and boxouts
Elements paras = content.select("p");
Element firstPara = paras.get(0);
recursiveDFS(firstPara);
System.out.println();
iterativeDFS(firstPara);
System.out.println();
Iterable<Node> iter = new WikiNodeIterable(firstPara);
for (Node node: iter) {
if (node instanceof TextNode) {
System.out.print(node);
}
}
}
private static void iterativeDFS(Node root) {
Deque<Node> stack = new ArrayDeque<Node>();
stack.push(root);
// if the stack is empty, we're done
while (!stack.isEmpty()) {
// otherwise pop the next Node off the stack
Node node = stack.pop();
if (node instanceof TextNode) {
System.out.print(node);
}
// push the children onto the stack in reverse order
List<Node> nodes = new ArrayList<Node>(node.childNodes());
Collections.reverse(nodes);
for (Node child: nodes) {
stack.push(child);
}
}
}
private static void recursiveDFS(Node node) {
if (node instanceof TextNode) {
System.out.print(node);
}
for (Node child: node.childNodes()) {
recursiveDFS(child);
}
}
}