从资产中读取xml文件

时间:2014-02-27 14:38:52

标签: android xml xmlpullparser

我的Eclipse android应用程序的assets文件夹中有一个xml文件,我想阅读它。

以下是文件中的文字:

<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>

这是我的代码:

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();
        }

现在这不起作用。首先输入START_DOCUMENT的case语句。在第二个循环中,输入TEXT大小写,parser.getName()的值为“text”。 “问题”正在被忽略。为什么会这样?

2 个答案:

答案 0 :(得分:0)

您缺少休息语句

case XmlPullParser.START_TAG:

break;

您可以使用以下内容作为参考。我希望代码是自我解释的。

如果您需要更多信息,请查看文档有一个很好的例子。

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;
    }
}

日志:

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

答案 1 :(得分:0)

发布代码的问题在于START_TAG案例正在落入TEXT案例。因此,在循环的第二次迭代中,遇到类型问题的START_TAG,但代码将其视为TEXT事件。

顺便说一句,为标签之间的每个字符串返回TEXT事件(而不是容易混淆的'text'元素)。要查看这意味着什么,请尝试使用此简化代码并检查日志。

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();
}

对于该代码片段,完整的第一个问题元素的日志输出如下所示:

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