XmlPullParser返回空值

时间:2012-04-02 03:38:21

标签: java xml-parsing

我的代码似乎可以解析xml文件,但gettext方法调用返回的值为null。我知道xml文件中有有效值。

public void xmlParser() throws XmlPullParserException, IOException,
            ParserConfigurationException, SAXException {

        Log.d("tag", "xmlParsing....");

        arthm = new Arithmetic();
        XmlPullParserFactory xmlPF = XmlPullParserFactory.newInstance();
        xmlPF.setNamespaceAware(true);
        xmlPF.setValidating(false);
        XmlPullParser xml = xmlPF.newPullParser();

        InputStream raw = getApplication().getAssets().open("menu.xml");
        xml.setInput(raw, null);
        xml.nextTag();

        Log.d("tag", "start parsing....");

        String elementText = null;
        Boolean done = false;
        while (xml.getEventType() != XmlPullParser.END_DOCUMENT && !done) {
            Log.d("tag", "beginning of while loop.");
            switch (xml.getEventType()) {
            case XmlPullParser.START_TAG:
                Log.d("tag", " case START_TAG");
                elementText = xml.getName();
                Log.d("tag", "<" + elementText + ">");
                xml.next();
                break;
            case XmlPullParser.TEXT:
                xml.next();
                Log.d("tag", "case TEXT");
                if (elementText.equals("id")) {
                    Log.d("tag", "<" + elementText + ">");
                    arthm.menu_name = xml.getText();
                    Log.d("tag", xml.getText() + " added");
                } else if (elementText.equals("name")) {
                    Log.d("tag", "<" + elementText + ">");
                    arthm.description = xml.getText();
                    Log.d("tag", xml.getText() + " added");
                } else if (elementText.equals("cost")) {
                    Log.d("tag", "<" + elementText + ">");
                    arthm.setSubCatergoryName(xml.getText());
                    Log.d("tag", xml.getText() + " added");
                } else if (elementText.equals("description")) {
                    Log.d("tag", "<" + elementText + ">");
                    arthm.setSubCategoryDesc(xml.getText());
                    Log.d("tag", xml.getText() + " added");
                }
                break;
            case XmlPullParser.END_TAG:
                Log.d("tag", elementText + " END_TAG");
                xml.next();

                break;
            case XmlPullParser.END_DOCUMENT:
                Log.d("tag", elementText + " END_DOCUMENT");
                break;
            }
        }
        Log.d("tag", "Success!");
    }

提前致谢。

0 个答案:

没有答案