Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How do I use xmlpullparser

Posted on 2010-09-16
5
Medium Priority
?
1,382 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
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.

610 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