-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathXmlParser.java
More file actions
140 lines (129 loc) · 5.06 KB
/
Copy pathXmlParser.java
File metadata and controls
140 lines (129 loc) · 5.06 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package ToArrayExcel;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
import java.io.StringReader;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.xml.sax.InputSource;
//import javax.xml.parsers.*;
import org.w3c.dom.*;
/**
*
* @author Administrator
*/
public class XmlParser {
enum TYPE {NONE, THROUGHPUT, TRUST, PARENT};
private DocumentBuilderFactory dbf;
private DocumentBuilder db;
private Document document;
private InputSource is;
private float sent, received;
public TYPE type;
public int source, target, nodeId, parentSource, parentTarget;
public long time;
public int transaction; /* Should be changed to double type */
public float throughput;
public int nodeValue, isGood;
public float trustFP, /*trustLP,*/ trustPR, overall;
public XmlParser() {
dbf = DocumentBuilderFactory.newInstance();
try {
db = dbf.newDocumentBuilder();
is = new InputSource();
} catch (Exception e) {
e.printStackTrace();
}
type = TYPE.NONE;
time = 0;
throughput = parentSource = parentTarget = source = target = nodeId = 0;
// grb = brb = gfb = bfb = gab = bab = glb = blb = 0;
nodeValue = -1;
trustFP = /*trustLP = */trustPR = overall = 0;
}
public boolean parse(String xml) {
try {
is.setCharacterStream(new StringReader(xml.trim()));
document = db.parse(is);
return parseXml();
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
private boolean parseXml() {
try {
if (document != null) {
NodeList list = document.getElementsByTagName("throughput");
if (list.getLength() != 0) {
type = TYPE.THROUGHPUT;
} else {
list = document.getElementsByTagName("trust");
if (list.getLength() != 0) {
type = TYPE.TRUST;
} else {
list = document.getElementsByTagName("parent");
if (list.getLength() != 0) {
type = TYPE.PARENT;
} else {
type = TYPE.NONE;
}
}
}
Element mainElement = (Element)list.item(0);
switch (type) {
case TRUST:
transaction = Integer.parseInt(mainElement.getAttribute("transaction"));
time = Integer.parseInt(mainElement.getAttribute("time"));
source = Integer.parseInt(mainElement.getAttribute("source"));
target = Integer.parseInt(mainElement.getAttribute("target"));
isGood = Integer.parseInt(mainElement.getAttribute("isGood"));
String temp;
temp = mainElement.getAttribute("trustFP");
if (!temp.equals("nan"))
trustFP = Float.parseFloat(mainElement.getAttribute("trustFP"));
else
trustFP = 0.0F;
/* temp = mainElement.getAttribute("trustLP");
if (!temp.equals("nan"))
trustLP = Float.parseFloat(mainElement.getAttribute("trustLP"));
else
trustLP = 0.0F;
temp = mainElement.getAttribute("trustPR");
*/
if (!temp.equals("nan"))
trustPR = Float.parseFloat(mainElement.getAttribute("trustPR"));
else
trustPR = 0.0F;
temp = mainElement.getAttribute("overallTrust");
if (!temp.equals("nan"))
overall = Float.parseFloat(mainElement.getAttribute("overallTrust"));
else
overall = 0.0F;
break;
case THROUGHPUT:
transaction = Integer.parseInt(mainElement.getAttribute("transaction"));
sent = Float.parseFloat(mainElement.getAttribute("sent"));
received = Float.parseFloat(mainElement.getAttribute("received"));
throughput = (received/sent) * 100;
break;
case PARENT:
transaction = Integer.parseInt(mainElement.getAttribute("transaction"));
parentSource = Integer.parseInt(mainElement.getAttribute("source"));
parentTarget = Integer.parseInt(mainElement.getAttribute("target"));
break;
case NONE:
default:
return false;
}
return true;
} else {
return false;
}
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
}