Link to home
Start Free TrialLog in
Avatar of nikomanek
nikomanekFlag for Afghanistan

asked on

XmlPullParser does not read a value

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);
                                }
                            }
                         

        });}}
             
             
Avatar of Kevin Cross
Kevin Cross
Flag of United States of America image

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.
ASKER CERTIFIED SOLUTION
Avatar of mccarl
mccarl
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
mccarl,

Thanks for chiming in.
https://www.experts-exchange.com/questions/26479953/How-do-I-use-xmlpullparser.html?anchorAnswerId=33698727#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