-
Notifications
You must be signed in to change notification settings - Fork 99
/
Copy pathDOMParser.java
112 lines (95 loc) · 2.98 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
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
110
111
112
import java.io.FileInputStream;
import java.io.IOException;
import java.util.LinkedList;
import java.util.ArrayList;
import java.util.List;
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();
}
List<Data> dataList = new ArrayList<Data>();
int[] counts = new int[12];
for (int i=0; i<12; i++) {
counts[i] = 0;
}
Element rootElement = document.getDocumentElement();
Queue<Element> q = new LinkedList<Element>();
q.add(rootElement);
String cameraStatus = "";
String ipCommStatus = "";
while(!q.isEmpty()) {
Element e = (Element) q.remove();
if (e.getNodeName().equals("camera_status")) {
String nodeValue = e.getTextContent();
//System.out.println("Node value:" + nodeValue);
cameraStatus = nodeValue;
}
if (e.getNodeName().equals("ip_comm_status")) {
String nodeValue = e.getTextContent();
//System.out.println("Node value:" + nodeValue);
ipCommStatus = nodeValue;
}
if (cameraStatus != "" && ipCommStatus != "") {
Data d = new Data(cameraStatus, ipCommStatus);
dataList.add(d);
cameraStatus = "";
ipCommStatus = "";
}
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);
}
}
}
System.out.println("DataList Size:" + dataList.size());
for (int i=0; i<dataList.size(); i++) {
Data d = dataList.get(i);
System.out.print(i + " Camera Status:" + d.cameraStatus + " ");
System.out.println(" IP Comm Status:" + d.ipCommStatus);
if (d.cameraStatus.equalsIgnoreCase("desired") && d.ipCommStatus.equalsIgnoreCase("online")) {
counts[0]++;
}
if (d.cameraStatus.equalsIgnoreCase("desired") && d.ipCommStatus.equalsIgnoreCase("offline")) {
counts[1]++;
}
if (d.cameraStatus.equalsIgnoreCase("desired") && d.ipCommStatus.equalsIgnoreCase("no communication")) {
counts[2]++;
}
}
for (int i=0; i<12; i++) {
System.out.println(counts[i]);
}
}
}