?
Solved

How do I use xmlpullparser

Posted on 2010-09-16
5
Medium Priority
?
1,373 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 36

Expert Comment

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

Author Comment

by:nikomanek
ID: 33698703
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 36

Accepted Solution

by:
mccarl earned 2000 total points
ID: 33698727
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
ID: 33698759
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

Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Suggested Courses
Course of the Month11 days, 3 hours left to enroll

770 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