Solved

How do I use xmlpullparser

Posted on 2010-09-16
5
1,337 Views
Last Modified: 2013-11-23
Hi there,

I am trying all day long to get a grasp about XmlPullParser. I just have to parse files like:

<?xml version="1.0" encoding="utf-8"?>
<NResultAuthorize xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><WebServiceUrl>http://xxx.xxx.com/rest/xxxx-1.0.php</WebServiceUrl><Success>true</Success></NResultAuthorize>

And I simply don't find a way to get the value "true" out of this string. Any code snipped would be very appreciated...
0
Comment
Question by:nikomanek
  • 2
  • 2
5 Comments
 
LVL 35

Expert Comment

by:mccarl
Comment Utility
Are you able to provide us with what you have already tried. It would be a good starting point to helping you out.
0
 
LVL 26

Expert Comment

by:ksivananth
Comment Utility
0
 

Author Comment

by:nikomanek
Comment Utility
Hi,

I stripped it down to the below...

Button go = (Button) findViewById(R.id.do_action);
        go.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                           try {
                            URL text = new URL("http://xx.xx.xx.210/REST/Service.php?method=XMLAuthorize&username=john&password=pass&type=XML");
                           
                           
                            XmlPullParserFactory parserCreator = XmlPullParserFactory.newInstance();
                            XmlPullParser parser = parserCreator.newPullParser();

                            parser.setInput(text.openStream(), null);

                         
                           
                            int eventType = parser.getEventType();
                           
                            while (eventType != XmlPullParser.END_DOCUMENT) {
                            if (eventType ==XmlResourceParser.TEXT){
                                  Log.d("", "Document Start");
                                  String strName = parser.getName();
                                  if (strName.equals("Success")){
                                        Log.d("","Found SUCCESS tag");
                                        Log.d("","The authvalue SUCCESS is"+parser.getAttributeValue(null, "Success"));
                                        
                                        
                                  }
                            }
                               
                                                };

                                     
                               

                                eventType = parser.next();
                                Log.d("","Did not find it");
                           
                           

                        } catch (Exception e) {
                            Log.e("Net", "Error in network call", e);
                        }
            }
});}
}
0
 
LVL 35

Accepted Solution

by:
mccarl earned 500 total points
Comment Utility
I am not able to test this code properly, but give it a try and see what you get. There may still be some errors in it but it will hopefully put you on the right track.

public void onClick(View v)
    {
        try
        {
            URL text = new URL("http://xx.xx.xx.210/REST/Service.php?method=XMLAuthorize&username=john&password=pass&type=XML");
            
            
            XmlPullParserFactory parserCreator = XmlPullParserFactory.newInstance();
            XmlPullParser parser = parserCreator.newPullParser();
            
            parser.setInput(text.openStream(), null);
            
            
            
            int eventType = parser.getEventType();
            boolean inSuccessTag = false;
            
            while (eventType != XmlPullParser.END_DOCUMENT)
            {
                if (eventType == XmlPullParser.START_TAG)
                {
                    if(parser.getName().equals("Success"))
                        inSuccessTag = true;
                }
                else if (eventType ==XmlPullParser.END_TAG)
                {
                    inSuccessTag = false;
                }
                else if (eventType ==XmlPullParser.TEXT)
                {
                    if (inSuccessTag)
                    {
                        Log.d("","Found SUCCESS tag");
                        Log.d("","The authvalue SUCCESS is"+parser.getText());
                        
                        
                    }
                }
                
                eventType = parser.next();
            }
        }
        catch (Exception e)
        {
            Log.e("Net", "Error in network call", e);
        }
    }

Open in new window

0
 

Author Closing Comment

by:nikomanek
Comment Utility
mccarl is my personal hero now:-) I was playing around with that an entire day being new to Java etc. This really saved my sanity.
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This video teaches viewers about errors in exception handling.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now