Solved

How do I use xmlpullparser

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
hibernate insert example 13 39
Html Table Looping (part 2) 5 35
wild fly 8 startup error 2 43
Using jdbcTemplate.batchUpdate to improve INSERT performance 6 27
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
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 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…
The viewer will learn how to implement Singleton Design Pattern in Java.

726 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