-
Notifications
You must be signed in to change notification settings - Fork 3
/
DOMParser.java
66 lines (56 loc) · 1.66 KB
/
DOMParser.java
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
import java.io.FileInputStream;
import java.io.IOException;
import java.util.LinkedList;
import java.util.Queue;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
public class DOMParser {
public static void main(String [] args) {
if (args.length < 1) {
System.out.println("Usage:");
System.out.println("java DOMParser <XML File>");
System.exit(0);
}
String fileName = args[0];
DocumentBuilderFactory builderFactory =
DocumentBuilderFactory.newInstance();
DocumentBuilder builder = null;
try {
builder = builderFactory.newDocumentBuilder();
} catch (ParserConfigurationException e) {
e.printStackTrace();
}
Document document = null;
try {
document = builder.parse(
new FileInputStream(fileName));
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Element rootElement = document.getDocumentElement();
Queue<Element> q = new LinkedList<Element>();
q.add(rootElement);
while(!q.isEmpty()) {
Element e = (Element) q.remove();
if (e.getNodeName().equals("assignment")) {
String nodeValue = e.getTextContent();
System.out.println("Node value:" + nodeValue);
}
NodeList nodes = e.getChildNodes();
for(int i=0; i<nodes.getLength(); i++) {
Node node = nodes.item(i);
if(node instanceof Element) {
q.add((Element) node);
}
}
}
}
}