How to get a specific node value in XML Pull Parser

1.1k views Asked by At

I am using the following code to parse XML, it has been referenced from the official Android Docs:

XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(true);
XmlPullParser xpp = factory.newPullParser();

xpp.setInput(new StringReader(response));
int eventType = xpp.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 " + xpp.getName());
    } else if (eventType == XmlPullParser.END_TAG) {
        System.out.println("End tag " + xpp.getName());
    } else if (eventType == XmlPullParser.TEXT) {
    //    System.out.println("Text " + xpp.getText());



    }
    eventType = xpp.next();
}
System.out.println("End document");

On the console:

06-08 11:13:25.557  24857-24883/ex.com.receipts I/System.out﹕ Start document
06-08 11:13:25.557  24857-24883/ex.com.receipts I/System.out﹕ Start tag exareceipts
06-08 11:13:25.557  24857-24883/ex.com.receipts I/System.out﹕ Start tag email
06-08 11:13:25.557  24857-24883/ex.com.receipts I/System.out﹕ End tag email
06-08 11:13:25.557  24857-24883/ex.com.receipts I/System.out﹕ Start tag authentication_status
06-08 11:13:25.557  24857-24883/ex.com.receipts I/System.out﹕ End tag authentication_status
06-08 11:13:25.557  24857-24883/ex.com.receipts I/System.out﹕ End tag exareceipts
06-08 11:13:25.557  24857-24883/ex.com.receipts I/System.out﹕ End document

However I am only interested in the node with name authentication_status for this I know I need to check:

if(xpp.getName().equalsIgnoreCase("authentication_status")){
    //logic for getting node value
}

I am really confused and unsure - where to place this code.

1

There are 1 answers

0
User3 On

Okay, here is how to do it:

 XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
                            factory.setNamespaceAware(true);
                            XmlPullParser xpp = factory.newPullParser();

                            String tagName = null;

                            xpp.setInput(new StringReader(response));
                            int eventType = xpp.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 " + xpp.getName());
                                    tagName =  xpp.getName();
                                } else if (eventType == XmlPullParser.END_TAG) {
                                    System.out.println("End tag " + xpp.getName());
                                } else if (eventType == XmlPullParser.TEXT) {

                                    if(tagName.equalsIgnoreCase("authentication_status")){
                                        System.out.println("Text tagName " + xpp.getText());
                                    }




                                }
                                eventType = xpp.next();
                            }
                            System.out.println("End document");