Wednesday, October 16, 2013

Parser XML via XmlPullParser in Android app

It's easy and simple to use XmlPullParser in Android app, the key method is next(). Call next() to go to next tag in XML, then adjust the current position by the event type. Just notice there is a TEXT event with empty value between two lines.

Here is how to parser a asset XML file:

            InputStream is = getAssets().open("colors.xml");
            XmlPullParser parser = Xml.newPullParser();
            parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
            parser.setInput(is, null);
            int eventType = parser.getEventType();
            while (eventType != XmlPullParser.END_DOCUMENT) {
                if (eventType == XmlPullParser.START_DOCUMENT) {
                    System.out.println("Start document");
                } else if (eventType == XmlPullParser.START_TAG) {
                    System.out.println("Start tag " + parser.getName());
                } else if (eventType == XmlPullParser.END_TAG) {
                    System.out.println("End tag " + parser.getName());
                } else if (eventType == XmlPullParser.TEXT) {
                    System.out.println("Text " + parser.getText());
                eventType =;
            System.out.println("End document");

No comments:

Post a Comment

Enable HP Zbook Thunderbolt 3 Dock on Ubuntu

As mention in previously post, I installed Ubuntu on my HP ZBook. After two days used all things work just fine, but the Thunderbolt 3 Dock ...