forked from Tamarik/CSCI4100_Spring12
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathRetrieveXML.java
179 lines (164 loc) · 6.58 KB
/
RetrieveXML.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import java.awt.List;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.*;
import org.xml.sax.SAXException;
public class RetrieveXML {
/**
* Default constructor
*/
private String dpath;
public RetrieveXML(String path) {
dpath = path;
}
/**
* Function to return question IDs for later output processing.
* @param section
* @return List of integer values of questions that are of the given difficulty
*/
public List returnBySection(double section){
List questionsToReturn = new List();
try {
DocumentBuilderFactory dBF = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = dBF.newDocumentBuilder();
Document database = docBuilder.parse(new File(dpath));
NodeList listOfQuestions = database.getElementsByTagName("question");
for(int i=0; i<listOfQuestions.getLength(); i++) {
Node firstQuestion = listOfQuestions.item(i);
Element firstQuestionE = (Element)firstQuestion;
NodeList sectionlist = firstQuestionE.getElementsByTagName("section");
Element firstSectionElement = (Element)sectionlist.item(0);
NodeList textSectionList = firstSectionElement.getChildNodes();
double sectionvalue = Double.valueOf(textSectionList.item(0).getNodeValue().trim());
if (sectionvalue == section) {
NodeList idlist = firstQuestionE.getElementsByTagName("id");
Element firstIDElement = (Element)idlist.item(0);
NodeList textIDList = firstIDElement.getChildNodes();
String questionID = textIDList.item(0).getNodeValue().trim();
questionsToReturn.add(questionID);
}
}
}catch(ParserConfigurationException pce) {
pce.printStackTrace();
}catch(SAXException se) {
se.printStackTrace();
}catch(IOException ioe) {
ioe.printStackTrace();
}
return questionsToReturn;
}
/**
* Function to return question IDs for later output processing.
* @param sectionTopic
* @return List of integer values of questions that are of the given difficulty
*/
public List returnByTopic(String sectionTopic){
List questionsToReturn = new List();
try {
DocumentBuilderFactory dBF = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = dBF.newDocumentBuilder();
Document database = docBuilder.parse(new File(dpath));
NodeList listOfQuestions = database.getElementsByTagName("question");
for(int i=0; i<listOfQuestions.getLength(); i++) {
Node firstQuestion = listOfQuestions.item(i);
Element firstQuestionE = (Element)firstQuestion;
NodeList topiclist = firstQuestionE.getElementsByTagName("subject");
Element firstTopicElement = (Element)topiclist.item(0);
NodeList textTopicList = firstTopicElement.getChildNodes();
String topicvalue = textTopicList.item(0).getNodeValue().trim();
if (topicvalue.compareTo(sectionTopic)==0){
NodeList idlist = firstQuestionE.getElementsByTagName("id");
Element firstIDElement = (Element)idlist.item(0);
NodeList textIDList = firstIDElement.getChildNodes();
String questionID = textIDList.item(0).getNodeValue().trim();
questionsToReturn.add(questionID);
}
}
}catch(ParserConfigurationException pce) {
pce.printStackTrace();
}catch(SAXException se) {
se.printStackTrace();
}catch(IOException ioe) {
ioe.printStackTrace();
}
return questionsToReturn;
}
/**
* Function to return question IDs for later output processing.
* @param difficulty
* @return List of integer values of questions that are of the given difficulty
*/
public List returnByDifficulty(int difficulty){
List questionsToReturn = new List();
try {
DocumentBuilderFactory dBF = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = dBF.newDocumentBuilder();
Document database = docBuilder.parse(new File(dpath));
NodeList listOfQuestions = database.getElementsByTagName("question");
for(int i=0; i<listOfQuestions.getLength(); i++) {
Node firstQuestion = listOfQuestions.item(i);
Element firstQuestionE = (Element)firstQuestion;
NodeList sectionlist = firstQuestionE.getElementsByTagName("difficulty");
Element firstDifficultyElement = (Element)sectionlist.item(0);
NodeList textDifficultyList = firstDifficultyElement.getChildNodes();
double difficultyValue = Double.valueOf(textDifficultyList.item(0).getNodeValue().trim());
if (difficultyValue == difficulty) {
NodeList idlist = firstQuestionE.getElementsByTagName("id");
Element firstIDElement = (Element)idlist.item(0);
NodeList textIDList = firstIDElement.getChildNodes();
String questionID = textIDList.item(0).getNodeValue().trim();
questionsToReturn.add(questionID);
}
}
}catch(ParserConfigurationException pce) {
pce.printStackTrace();
}catch(SAXException se) {
se.printStackTrace();
}catch(IOException ioe) {
ioe.printStackTrace();
}
return questionsToReturn;
}
/**
* Function to return specified test data from a specific question. Any tag may be requested from the question element
* @param questionID
* @param tagname
* @return String with the requested test data
*/
public String returnTestData(String questionID, String tagname) {
String returnvalue = "";
try {
DocumentBuilderFactory dBF = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = dBF.newDocumentBuilder();
Document database = docBuilder.parse(new File(dpath));
while(questionID.charAt(0)=='0')
questionID = questionID.substring(1);
NodeList listOfQuestions = database.getElementsByTagName("question");
for(int i=0; i<listOfQuestions.getLength(); i++) {
Node firstQuestion = listOfQuestions.item(i);
Element firstQuestionE = (Element)firstQuestion;
NodeList sectionlist = firstQuestionE.getElementsByTagName("id");
Element firstIDElement = (Element)sectionlist.item(0);
NodeList textIDList = firstIDElement.getChildNodes();
Integer IDvalue = Integer.valueOf(textIDList.item(0).getNodeValue().trim());
if (IDvalue.toString().compareTo(questionID)==0) {
NodeList returnList = firstQuestionE.getElementsByTagName(tagname);
Element ReturnElement = (Element)returnList.item(0);
NodeList textReturnList = ReturnElement.getChildNodes();
returnvalue = textReturnList.item(0).getNodeValue().trim();
}
}
}catch(ParserConfigurationException pce) {
pce.printStackTrace();
}catch(SAXException se) {
se.printStackTrace();
}catch(IOException ioe) {
ioe.printStackTrace();
}
return returnvalue;
}
}