-
Notifications
You must be signed in to change notification settings - Fork 0
/
readXmlFile.java
156 lines (125 loc) · 5.07 KB
/
readXmlFile.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
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
/**
* This class imports all the information from the given xml file.
* We will extract information in order to later create out variables and network.
* @author Maya
*
*/
public class readXmlFile {
private Document doc;
public readXmlFile(String xmlName) {
try {
File xmlDoc = new File (xmlName);
DocumentBuilderFactory dbFact = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuild = dbFact.newDocumentBuilder();
doc = dBuild.parse(xmlDoc);
doc.getDocumentElement();
}
catch(Exception e) {
e.printStackTrace();
}
}
/**
* This function gets all names from xml in order to create the list of variables in out network.
* @return a list of all given names (all variables names).
*/
public ArrayList<String> getNames() {
// Note that the names are found under the VARIABLE line, therefore we need to access that first
NodeList namesNode = doc.getElementsByTagName("VARIABLE");
ArrayList<String> names = new ArrayList<String>();
//Go through all names found in file
for(int i=0; i < namesNode.getLength(); i++){
Node name = namesNode.item(i);
if(name.getNodeType() == Node.ELEMENT_NODE){
Element eName = (Element) name;
//Then we will access the NAME lines
String vName = eName.getElementsByTagName("NAME").item(0).getTextContent();
names.add(vName);
}
}
return names;
}
/**
* This function goes through the xml file in order to find the possible outcomes of each variable.
* @param variable
* @return an arraylist of all variable's possible outcomes.
*/
public ArrayList<String> getOutcomes(String variable) {
//Note in file that the possible outcomes are also found underneath VARIABLE
NodeList outcomeNode = doc.getElementsByTagName("VARIABLE");
ArrayList<String> outcomes = new ArrayList<String>();
for(int i=0; i < outcomeNode.getLength(); i++){
Node outcome = outcomeNode.item(i);
if(outcome.getNodeType() == Node.ELEMENT_NODE){
Element eOutcomes = (Element) outcome;
String vOutcome = eOutcomes.getElementsByTagName("NAME").item(0).getTextContent();
//find the outcomes of a given variable
if(variable.equals(vOutcome)) {
NodeList outcomesOfVariable = eOutcomes.getElementsByTagName("OUTCOME");
for (int j = 0 ; j < outcomesOfVariable.getLength(); j++)
outcomes.add(outcomesOfVariable.item(j).getTextContent());
}
}
}
return outcomes;
}
/**
* This function goes through the xml file in order to find the all the probabilities of each variable.
* @param variable
* @return an arraylist of probabilities of a specific variable
* Note that we will need to convert it later from string to double.
*/
public ArrayList<String> getProbabilities(String variable) {
//Note that the probabilities(TABLE) are in the DEFINITION section
NodeList probNode = doc.getElementsByTagName("DEFINITION");
ArrayList<String> probabilities = new ArrayList<String>();
for(int i=0; i < probNode.getLength(); i++){
Node prob = probNode.item(i);
if(prob.getNodeType() == Node.ELEMENT_NODE){
Element eProbs = (Element) prob;
//TABLE is found in the same level as FOR
String vProb = eProbs.getElementsByTagName("FOR").item(0).getTextContent();
//Find the probabilities of a given variable by String.
if(variable.equals(vProb)) {
NodeList probabilitiessOfVariable = eProbs.getElementsByTagName("TABLE");
for (int j = 0 ; j < probabilitiessOfVariable.getLength(); j++)
probabilities.add(probabilitiessOfVariable.item(j).getTextContent());
}
}
}
//note that this returns all probabilities as a string in the first cell of the arraylist, we need to split later
return probabilities;
}
/**
* This function goes through the xml file in order to find the all the parents of each variable.
* @param variable
* @return an arraylist of parents of a specific variable
*/
public ArrayList<String> getParents(String child) {
//Note that the parents(GIVEN) are in the DEFINITION section
NodeList parentsNode = doc.getElementsByTagName("DEFINITION");
ArrayList<String> parents = new ArrayList<String>();
for(int i=0; i < parentsNode.getLength(); i++){
Node parent = parentsNode.item(i);
if(parent.getNodeType() == Node.ELEMENT_NODE){
Element eParents = (Element) parent;
//we need to use the next line as it is found in the same level as FOR
String vParent = eParents.getElementsByTagName("FOR").item(0).getTextContent();
if(child.equals(vParent)) {
NodeList parentsOfVariable = eParents.getElementsByTagName("GIVEN");
for (int j = 0 ; j < parentsOfVariable.getLength(); j++)
parents.add(parentsOfVariable.item(j).getTextContent());
}
}
}
return parents;
}
}