-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathXmlFormatStrategy.java
More file actions
62 lines (49 loc) · 2.36 KB
/
XmlFormatStrategy.java
File metadata and controls
62 lines (49 loc) · 2.36 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
import java.io.*;
import java.util.ArrayList;
import javax.xml.parsers.*;
import org.w3c.dom.*;
public class XmlFormatStrategy implements FileFormatStrategy {
@Override
public ArrayList<Question> loadQuestions(String filePath) {
ArrayList<Question> questions = new ArrayList<>();
try {
File xmlFile = new File(filePath);
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(xmlFile);
doc.getDocumentElement().normalize();
NodeList questionNodes = doc.getElementsByTagName("question");
for (int i = 0; i < questionNodes.getLength(); i++) {
Node node = questionNodes.item(i);
if (node.getNodeType() == Node.ELEMENT_NODE) {
Element element = (Element) node;
String category = getTagValue("category", element);
int value = Integer.parseInt(getTagValue("value", element));
String content = getTagValue("content", element);
String correctAnswer = getTagValue("correctAnswer", element);
Question question = new Question(category, value, content, null, correctAnswer);
// Parse options if present
NodeList optionNodes = element.getElementsByTagName("option");
for (int j = 0; j < optionNodes.getLength(); j++) {
String option = optionNodes.item(j).getTextContent();
question.addOption(option);
}
questions.add(question);
}
}
} catch (Exception e) {
System.err.println("Error loading XML file: " + e.getMessage());
}
return questions;
}
private String getTagValue(String tag, Element element) {
NodeList nodeList = element.getElementsByTagName(tag);
if (nodeList.getLength() > 0) {
Node node = nodeList.item(0);
if (node != null) {
return node.getTextContent();
}
}
return "";
}
}