Solved

How do I use xmlpullparser

Posted on 2010-09-16
5
1,344 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
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
 
LVL 26

Expert Comment

by:ksivananth
ID: 33698702
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 35

Accepted Solution

by:
mccarl earned 500 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
allswap challenge 6 76
simple java question 3 44
Java DateChooser? 3 29
servlet requestdispatcher include and forward differences 1 22
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

919 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

20 Experts available now in Live!

Get 1:1 Help Now