I'm creating a simple trivia game for the Android platform. (For what it's worth, I'm a hobbyist who only gets time to code when I'm able to carve out free time. Thus, my question -- like my skillset -- is very basic.)
My game works if I hard code all of the questions on the QuestionActivity page, but this is obviously not a good approach in the long run.
I have created a Question object that includes the following properties: String question, String[] answers, int correctPointer, String pithyComment. Every Question object has only 4 possible answers. On the QuestionActivity page, I created a questionArray that includes several Question objects. I can shuffle items in the array, I can shuffle possible answers before they're displayed to the user, and all of the error checking works just fine.
I have also created an XML file that contains 50+ questions. I would like to load the contents of this file into a QuestionBank array of Question objects when the game is initialized. I have been researching this for the past several days, and I can't figure out the best way to move forward. Parsing XML in Android/Java seems more complicated than it has been in other programming languages in the past. Every XML parsing tutorial seems to end up requiring the creation of multiple classes in order to handle this very simple data structure. I wonder if there's something super obvious, and more "baked in," that I'm simply not seeing.
My question is this: Should I parse the XML file with SAX, with STAX, or via a DOM parser. Or, given the fact that this game will never have more than 500 questions, should I consider using SQL Lite instead? Or is there a better way to do this?
I don't need to write to XML, and I don't need to do any fancy parsing other than passing contents of one tag into the appropriate variable. The only tricky thing possibly related to parsing is the fact that there are four possible answers for each question. I have two versions of the XML file: one in which each answer exists at the same level as the other items (question, correctPointer) and one in which the four answers are contained with a parent tag named answerList. At this point, since there are always four answers and they always appear in the same sequence, I'm leaning toward just coding all four answers at the same level as the other items within XML and then manually pushing them into an array as I read them into the QuestionBank.
Thanks.
Here's a good tutorial on DOM Parser. It's simple, not too verbose and you won't need to create multiple classes to use it.
For example (from the tutorial):
<?xml version="1.0"?>
<company>
<staff id="1001">
<firstname>yong</firstname>
<lastname>mook kim</lastname>
<nickname>mkyong</nickname>
<salary>100000</salary>
</staff>
<staff id="2001">
<firstname>low</firstname>
<lastname>yin fong</lastname>
<nickname>fong fong</nickname>
<salary>200000</salary>
</staff>
</company>
How to use the parser:
try {
File fXmlFile = new File("/Users/mkyong/staff.xml");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(fXmlFile);
//optional, but recommended
//read this - http://stackoverflow.com/questions/13786607/normalization-in-dom-parsing-with-java-how-does-it-work
doc.getDocumentElement().normalize();
System.out.println("Root element :" + doc.getDocumentElement().getNodeName());
NodeList nList = doc.getElementsByTagName("staff");
System.out.println("----------------------------");
for (int temp = 0; temp < nList.getLength(); temp++) {
Node nNode = nList.item(temp);
System.out.println("\nCurrent Element :" + nNode.getNodeName());
if (nNode.getNodeType() == Node.ELEMENT_NODE) {
Element eElement = (Element) nNode;
System.out.println("Staff id : " + eElement.getAttribute("id"));
System.out.println("First Name : " + eElement.getElementsByTagName("firstname").item(0).getTextContent());
System.out.println("Last Name : " + eElement.getElementsByTagName("lastname").item(0).getTextContent());
System.out.println("Nick Name : " + eElement.getElementsByTagName("nickname").item(0).getTextContent());
System.out.println("Salary : " + eElement.getElementsByTagName("salary").item(0).getTextContent());
}
}
} catch (Exception e) {
e.printStackTrace();
}
}