1

I have this xml:

<xml><result>-1</result></xml>

and following java code:

public String findElement(String xml, String elem) {
    try {
        XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
        factory.setNamespaceAware(true);
        XmlPullParser xpp = factory.newPullParser();

        xpp.setInput( new StringReader (xml) );

        while (xpp.getEventType() != XmlPullParser.END_DOCUMENT) {
            if (xpp.getName().equals(elem)) {
                return xpp.getText();
            }
            xpp.next();
        }
    } catch (XmlPullParserException | IOException e) {
        e.printStackTrace();
    }
    return null;
}

But xpp.getName() returns null instead of "result". As result NullPointerException is throwed.

Where am I wrong?

4
  • Try to check for null and continue if so. What happens if you do? Commented Dec 10, 2015 at 8:57
  • Maybe you can "steal" from here: androidcookbook.com/Recipe.seam?recipeId=2217 As I understand, not every event has the Name field set and you'll need probably "TEXT" event type. Commented Dec 10, 2015 at 9:00
  • Update: StartTag and EndTag events have Name set, TEXT does not. You may want to adopt the recipe. Commented Dec 10, 2015 at 9:03
  • Unexpected token (position:TEXT @1:2 in java.io.StringReader@5371c6a0) after checking for null Commented Dec 10, 2015 at 9:10

2 Answers 2

1

Change your code like this.

        int eventType = xpp.getEventType();
        while (eventType != XmlPullParser.END_DOCUMENT) {
            if (eventType == XmlPullParser.START_TAG) {
                if (xpp.getName().equals(elem)) {
                    eventType = xpp.next(); // advance to inner text
                    return xpp.getText();
                }
            }
            eventType = xpp.next();
        }
Sign up to request clarification or add additional context in comments.

Comments

1

XmlPullParser doc

Of course you need to check some more conditions like what if there is no text or tag consist another nested tag...

public String findElement(String xml, String elem) {
    try {
      XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
      factory.setNamespaceAware(true);
      XmlPullParser xpp = factory.newPullParser();

      xpp.setInput(new StringReader(xml));
      while (xpp.getEventType() != XmlPullParser.END_DOCUMENT) {

        final int event = xpp.getEventType();
        if (event == XmlPullParser.START_TAG && xpp.getName().equals(elem)) {
          xpp.next();
          return xpp.getText();
        }
        xpp.next();

      }
    } catch (XmlPullParserException | IOException e) {
      e.printStackTrace();
    }
    return null;
  }

and this

        if (event == XmlPullParser.START_TAG && xpp.getName().equals(elem)) {
      xpp.next();
      if (xpp.getEventType() == XmlPullParser.TEXT) {
        return xpp.getText();
      }
    }else{
      xpp.next();
    }

would work for something nested like

"<xml><result><noway><result>-1</result></noway></result></xml>"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.