Solved

XmlPullParser does not read a value

Posted on 2010-09-17
3
1,539 Views
Last Modified: 2013-11-23
Hi there,

I posted a question yesterday of how to read a XML stream and got the below answer which does what I asked. Unfortunately I asked the question wrong. I can find the "Success" tag in the XML but the main thing is to read it's value which is "true" or "false".  It simply does not read the content somehow... (I addedd the "bold" part to try it)

try
      {
      URL text = new URL("http://xxx.xxx.xxx.xxx/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")&& parser.getText()=="true")       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);
                                }
                            }
                         

        });}}
             
             
0
Comment
Question by:nikomanek
  • 2
3 Comments
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 33706709
Think the issue may be here:
  else if (eventType ==XmlPullParser.END_TAG)
       {
       inSuccessTag = false;
       }

Since you are in a loop and the parser has to read the START_TAG and END_TAG, what will happen is that you will set success to value from the tag, then no matter what you are going to set the value back to false when the end of the tag is read.
0
 
LVL 35

Accepted Solution

by:
mccarl earned 500 total points
ID: 33715467
Firstly, just letting you know that EE has an 'ask a related question' functionality that would have been good here so that I (and others following the last question) get a notification of the new question. I only stumbled upon this question by accident.

Also, while we are on EE features, you should use the option to attach code rather than just pasting it in the body of the question. That way it gives line number and nice formatting, etc.

Anyway, no the code that you have added will not work at all. You are trying to do 2 things at the one time. You can't test for the Succcess tag and get the text WITHIN the tag at the same time. They are two different events that will happen in two different iterations through the loop.

>>  I can find the "Success" tag in the XML but the main thing is to read it's value which is "true" or "false".
Did the code that I posted not include the value of the Success tag (i.e. the true or false part) in the log message?

>> Log.d("","The authvalue SUCCESS is"+parser.getText());
The above line in the code should have printed the value to the log (the parser.getText() is what should get the "true" or "false", or whatever is in the file)

0
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 33717239
mccarl,

Thanks for chiming in.
http://www.experts-exchange.com/Q_26479953.html#a33698727

I see what I wasn't catching due to the formatting of the code above (as you mentioned not being in a code block) is that the  if (inSuccessTag) { ... } test is also in the loop -- it had appeared to me as being outside the loop, hence my comment -- I see from your original code, you can setting flag on start of tag, then reading text, then on close of tag resetting the flag for the next iteration through loop.  I see now what was added was the check of parset.getText() in the start tag check.

Nice catch.  I will leave this in your capable hands.

M-1
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

As the title indicates, I have done this before. It chills me everytime I update the OS on my phone, (http://www.experts-exchange.com/articles/18084/Upgrading-to-Android-5-0-Lollipop.html) because one time I did this and I essentially had a bricked …
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This video teaches viewers about errors in exception handling.

747 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

8 Experts available now in Live!

Get 1:1 Help Now