0

我有一个 .xml 文件,如下所示:

<entry>
    <question>Which is larger? An elephant or a cat?</question>
    <answer>Elephant</answer>
</entry>

我正在学习教程并尝试将 .xml 文件中的数据解析为 TextViews。但是,在以下代码中,删除 else if 块(位于第二个 while 循环下)将允许应用程序正常运行,但会导致应用程序无响应。我已经跟踪了代码,但我仍然无法确定错误的原因。

    TextView tvQuestion = (TextView) findViewById(R.id.tvQuestion);
    TextView tvAnswer = (TextView) findViewById(R.id.tvAnswer);

    XmlResourceParser parser = getResources().getXml(R.xml.qa);

    try
    {
        while(parser.next() != XmlPullParser.END_TAG)
        {
            if(parser.getEventType() != XmlPullParser.START_TAG)
            {
                continue;
            }

            String name = parser.getName();

            if(name.equals("entry"))
            {
                String question = "";
                String answer = "";

                while(parser.next() != XmlPullParser.END_TAG)
                {
                    if(parser.getEventType() != XmlPullParser.START_TAG)
                    {
                        continue;
                    }

                    name = parser.getName();

                    if(name.equals("question"))
                    {
                        question = readText(parser);
                        tvQuestion.setText(question);
                    }
                    else if(name.equals("answer"))
                    {
                        answer = readText(parser);
                        tvAnswer.setText(answer);
                    }
                }
            }
        }
    }
    catch(XmlPullParserException e)
    {
        e.printStackTrace();
    }
    catch(IOException e)
    {
        e.printStackTrace();
    }
}

private String readText(XmlPullParser parser) throws XmlPullParserException, IOException
{
    String result = "";
    if(parser.next() == XmlPullParser.TEXT)
    {
        result = parser.getText();
        parser.nextTag();
    }

    return result;
}
4

1 回答 1

0

尝试这个 !

           DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder dBuilder;
    try {
        dBuilder = dbFactory.newDocumentBuilder();

        Document doc = dBuilder.parse(new InputSource(new ByteArrayInputStream(xml.getBytes("utf-8"))));
        doc.getDocumentElement().normalize();


        NodeList nList = doc.getElementsByTagName("entry");
        for (int temp = 0; temp < nList.getLength(); temp++) {

             Element element = (Element) nList.item(temp);

                 NodeList q= element.getElementsByTagName("question");
                 Element line = (Element) q.item(0);

                             String question = getCharacterDataFromElement(line);
                             tvQuestion.setText(question);

                 NodeList a = element.getElementsByTagName("answer");
                 line = (Element) a.item(0);

                             String answer = getCharacterDataFromElement(line);
                             tvAnswer.setText(answer);

        }
               } catch (Exception e) {

               }

public static String getCharacterDataFromElement(Element e) {
    Node child = e.getFirstChild();
    if (child instanceof CharacterData) {
      CharacterData cd = (CharacterData) child;
      return cd.getData();
    }
    return "";
}
于 2013-08-07T06:16:25.800 回答