Parsing xml files locally from assets folder using XmlPullParser

Posted by Randolphg on Super User See other posts from Super User or by Randolphg
Published on 2012-04-01T23:24:09Z Indexed on 2012/04/01 23:32 UTC
Read the original article Hit count: 503

Filed under:
|

Im trying to parse a local xml file that I place in my assets folder. I've been trying to do this for almost a week now.

Here is my test xml file

Test1 Test2 Test3 Test4 Test5

I keep getting the same error:

W/System.err(22458): org.xmlpull.v1.XmlPullParserException: unexpected type (position:TEXT

Code:

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

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

    Arithmetic arthm = new Arithmetic();
    XmlPullParserFactory xmlPF = XmlPullParserFactory.newInstance();
    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;
    String elemName = null;
    int nofTags = 0;
    while (xml.getEventType() != XmlPullParser.END_DOCUMENT) {
        Log.d("tag", "while(xml.next)...");
        switch (xml.getEventType()) {
        case XmlPullParser.START_DOCUMENT:
            Log.d("tag",
                    "while (xml.getEventType() != XmlPullParser.END_DOCUMENT)");
            break;
        case XmlPullParser.START_TAG:
            Log.d("tag", " case XmlPullParser.START_TAG");
            elementText = xml.getName();
            Log.d("tag", "elementText = " + elementText);
            if (xml.getEventType() != XmlPullParser.END_TAG) {
                xml.nextTag();
            }
            break;
        case XmlPullParser.TEXT:
            Log.d("tag", "case TEXT");
            if (elementText.equals("menu") && xml.isWhitespace()) {
                Log.d("tag", "<" + elementText + ">");
                arthm.menu_name = xml.getText();
                Log.d("tag", "value " + xml.getText() + " added");
            } else if (elementText.equals("item")) {
                arthm.description = xml.getText();
                Log.d("tag", "value " + xml.getText() + " added");
            } else if (elementText.equals("SUBCATEGORY NAME")) {
                arthm.subcategoryDesc.add(xml.getText());
                Log.d("tag", "value " + xml.getText() + " added");
            } else if (elementText.equals("SUBCATEGORY DESC")) {
                arthm.subcategoryName.add(xml.getText());
                Log.d("tag", "value " + xml.getText() + " added");
            }
            break;
        case XmlPullParser.END_TAG:
            Log.d("tag", "case END_TAG");
            nofTags += 1;
            String tags = Integer.toString(nofTags);
            Log.d("tags", elementText + " number of  tags" + tags);
            if (xml.nextTag() != XmlPullParser.START_TAG) {
                xml.next();
            }
            break;
        case XmlPullParser.END_DOCUMENT:
            Log.d("tag", "case END_DOCUMENT");

            break;
        default:
            break;
        }
    }
    Log.d("tag", "Success!");
}

Thanks in advance.

© Super User or respective owner

Related posts about android

Related posts about Xml