-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathXMLParser.java
More file actions
49 lines (42 loc) · 1.69 KB
/
Copy pathXMLParser.java
File metadata and controls
49 lines (42 loc) · 1.69 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
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;
import javax.xml.XMLConstants;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class XMLParser {
private static final String FILENAME = "input.xml";
DocumentBuilderFactory dbf;
public XMLParser() {
dbf = DocumentBuilderFactory.newInstance();
}
public List<Question> readInput() {
List<Question> questions = new ArrayList<>();
try {
dbf.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(new File(FILENAME));
doc.getDocumentElement().normalize();
NodeList list = doc.getElementsByTagName("question");
for (int index = 0; index < list.getLength(); index++) {
Node node = list.item(index);
if (node.getNodeType() == Node.ELEMENT_NODE) {
Element element = (Element) node;
String questiontype = element.getAttribute("type");
QuestionFactory factory = new QuestionFactory();
questions.add(factory.createQuestion(questiontype, element));
}
}
} catch (ParserConfigurationException | SAXException | IOException e) {
e.printStackTrace();
}
return questions;
}
}