I have an xml file in the assets folder in my Eclipse android application and I want to read it.
Here's the text in the file:
<question>
<text>Most?</text>
<answer correct="false">10</answer>
<answer correct="true">11</answer>
<answer correct="false">8</answer>
<answer correct="false">9</answer>
</question>
<question>
<text>Which?</text>
<answer correct="false">Titanic</answer>
<answer correct="false">American</answer>
<answer correct="true">:King</answer>
<answer correct="false">Yes</answer>
</question>
<question>
<text>First?</text>
<answer correct="false">John</answer>
<answer correct="true">James</answer>
<answer correct="false">Peter</answer>
<answer correct="false">Jean</answer>
</question>
Here's my code:
while (eventType != XmlPullParser.END_DOCUMENT){
String name = null;
switch (eventType){
case XmlPullParser.START_DOCUMENT:
myItems = new ArrayList();
break;
case XmlPullParser.START_TAG:
case XmlPullParser.TEXT:
name = parser.getName();
if (name != null && answers == null && myItem == null) {
answers = new ArrayList();
myItem = new MyItem();
}
if (name.equals("text")){
myItem.setQuestion(parser.nextText());
} else if (name.equals("answer")){
answers.add(new Answer(parser.nextText(), false));
myItem.setAnswers(answers);
}
break;
case XmlPullParser.END_TAG:
name = parser.getName();
if (name.equalsIgnoreCase("question") && quizItem != null){
quizItems.add(quizItem);
}
}
eventType = parser.next();
}
Now this doesn't work. First the case statement for the START_DOCUMENT is entered. On the second loop, the TEXT case is entered and the value of parser.getName() is "text". "question" is being skipped. Why is this happening?