-1

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?

Charles
  • 50,943
  • 13
  • 104
  • 142
petko_stankoski
  • 10,459
  • 41
  • 127
  • 231

2 Answers2

0

You are missing a break statement

case XmlPullParser.START_TAG:

break;

You can use the below for reference. I hope the code is self explanatory.

If you need more info check the docs has a nice exmaple.

http://developer.android.com/training/basics/network-ops/xml.html

public class XMLPullParserHandler {

    private String text;

    public XMLPullParserHandler() {

    }

    public Void parse(InputStream is) { // pass input stream
        XmlPullParserFactory factory = null;
        XmlPullParser parser = null;
        try {
            factory = XmlPullParserFactory.newInstance();
            factory.setNamespaceAware(true);
            parser = factory.newPullParser();

            parser.setInput(is, null);

            int eventType = parser.getEventType();
            while (eventType != XmlPullParser.END_DOCUMENT) {
                String tagname = parser.getName();
                switch (eventType) {
                case XmlPullParser.START_TAG:
                    if (tagname.equalsIgnoreCase("question")) {

                    }
                    else if (tagname.equalsIgnoreCase("answer")) { // is answer get the attribute
                        Log.i("Attribute true/false is",""+parser.getAttributeValue(null, "correct"));
                    }
                    break;

                case XmlPullParser.TEXT:
                    text = parser.getText();
                    break;

                case XmlPullParser.END_TAG: // end tag
                    if (tagname.equalsIgnoreCase("question")) {
                        // add employee object to list

                    } else if (tagname.equalsIgnoreCase("text")) { // if text log the vlaue
                        Log.i("Question  is",text);
                    } else if (tagname.equalsIgnoreCase("answer")) { // if answer log the value

                        Log.i("answer is",text);
                    }
                    break;

                default:
                    break;
                }
                eventType = parser.next();
            }

        } catch (XmlPullParserException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return null;
    }
}

The Log:

02-27 10:36:09.976: I/Question  is(1955): Most?
02-27 10:36:09.976: I/Attribute true/false is(1955): false
02-27 10:36:09.976: I/answer is(1955): 10
02-27 10:36:09.986: I/Attribute true/false is(1955): true
02-27 10:36:09.986: I/answer is(1955): 11
02-27 10:36:09.986: I/Attribute true/false is(1955): false
02-27 10:36:09.986: I/answer is(1955): 8
02-27 10:36:09.986: I/Attribute true/false is(1955): false
02-27 10:36:09.986: I/answer is(1955): 9
02-27 10:36:09.986: I/Question  is(1955): Which?
02-27 10:36:09.986: I/Attribute true/false is(1955): false
02-27 10:36:09.986: I/answer is(1955): Titanic
02-27 10:36:09.986: I/Attribute true/false is(1955): false
02-27 10:36:09.986: I/answer is(1955): American
02-27 10:36:09.986: I/Attribute true/false is(1955): true
02-27 10:36:09.986: I/answer is(1955): :King
02-27 10:36:09.996: I/Attribute true/false is(1955): false
02-27 10:36:09.996: I/answer is(1955): Yes
02-27 10:36:09.996: I/Question  is(1955): First?
02-27 10:36:09.996: I/Attribute true/false is(1955): false
02-27 10:36:09.996: I/answer is(1955): John
02-27 10:36:09.996: I/Attribute true/false is(1955): true
02-27 10:36:09.996: I/answer is(1955): James
02-27 10:36:09.996: I/Attribute true/false is(1955): false
02-27 10:36:09.996: I/answer is(1955): Peter
02-27 10:36:09.996: I/Attribute true/false is(1955): false
02-27 10:36:09.996: I/answer is(1955): Jean
Raghunandan
  • 132,755
  • 26
  • 225
  • 256
0

The problem with the posted code is that the START_TAG case is falling through to the TEXT case. So on the second iteration of the loop, a START_TAG of type question is encountered, but the code is treating it like a TEXT event.

By the way, TEXT events (as opposed to the confusingly named 'text' elements) are returned for each string of characters between tags. To see what this means, try this simplified code and inspect the log.

while (eventType != XmlPullParser.END_DOCUMENT) {
    Log.i(TAG, String.format("Starting new iteration with name [%s]", xpp.getName()));
    switch (eventType){
        case XmlPullParser.START_DOCUMENT:
            Log.i(TAG, "  found START_DOCUMENT");
            break;
        case XmlPullParser.START_TAG:                        
            Log.i(TAG, "  found START_TAG");
            break;
        case XmlPullParser.TEXT:
            Log.i(TAG, String.format("  found TEXT with [%s]", xpp.getText()));
            break;
        case XmlPullParser.END_TAG:
            Log.i(TAG, "  found END_TAG");
    }
    eventType = xpp.next();
}

For the that code snippet, the log output for the full first question element looks like this:

Starting new iteration with name [null]
  found START_DOCUMENT
Starting new iteration with name [question]
  found START_TAG
Starting new iteration with name [null]
  found TEXT with [
    ]
Starting new iteration with name [text]
  found START_TAG
Starting new iteration with name [null]
  found TEXT with [Most?]
Starting new iteration with name [text]
  found END_TAG
Starting new iteration with name [null]
  found TEXT with [
    ]
Starting new iteration with name [answer]
  found START_TAG
Starting new iteration with name [null]
  found TEXT with [10]
Starting new iteration with name [answer]
  found END_TAG
Starting new iteration with name [null]
  found TEXT with [
    ]
Starting new iteration with name [answer]
  found START_TAG
Starting new iteration with name [null]
  found TEXT with [11]
Starting new iteration with name [answer]
  found END_TAG
Starting new iteration with name [null]
  found TEXT with [
    ]
Starting new iteration with name [answer]
  found START_TAG
Starting new iteration with name [null]
  found TEXT with [8]
Starting new iteration with name [answer]
  found END_TAG
Starting new iteration with name [null]
  found TEXT with [
    ]
Starting new iteration with name [answer]
  found START_TAG
Starting new iteration with name [null]
  found TEXT with [9]
Starting new iteration with name [answer]
  found END_TAG
Starting new iteration with name [null]
  found TEXT with [
]
Starting new iteration with name [question]
  found END_TAG
scottt
  • 8,301
  • 1
  • 31
  • 41