Note that xmlpullparser. nexttext () of Android ()

Source: Internet
Author: User


Watch out for xmlpullparser. nexttext () posted by Tim Bray on 19 December 2011 at 10: 55 am

[This post is by Jesse Wilson from the Dalvik team.-Tim Bray]

Xmlpullparser is an efficient and maintainable way to parse XML on Android. Historically android has had two implementations of this interface:

  • Kxmlparser,
    Xmlpullparserfactory. newpullparser ().

  • Expatpullparser,
    XML. newpullparser ().

The implementation fromXml.newPullParser()Had a bug where calltonextText()Didn't always advance to
END_TAGAs the documentation promised it wocould. As a consequence, some apps may be working around the bug with extra calltonext()Or

    public void parseXml(Reader reader)            throws XmlPullParserException, IOException {        XmlPullParser parser = Xml.newPullParser();        parser.setInput(reader);        parser.nextTag();        parser.require(XmlPullParser.START_TAG, null, "menu");        while (parser.nextTag() == XmlPullParser.START_TAG) {            parser.require(XmlPullParser.START_TAG, null, "item");            String itemText = parser.nextText();            parser.nextTag(); // this call shouldn’t be necessary!            parser.require(XmlPullParser.END_TAG, null, "item");            System.out.println("menu option: " + itemText);        }        parser.require(XmlPullParser.END_TAG, null, "menu");    }    public static void main(String[] args) throws Exception {        new Menu().parseXml(new StringReader("<?xml version='1.0'?>"                + "<menu>"                + "  <item>Waffles</item>"                + "  <item>Coffee</item>"                + "</menu>"));    }

In ice cream sandwich we changedXml.newPullParser()To return a kxmlparser and deleted our expatpullparser class. This fixesnextTag()Bug. Unfortunately, apps that currently work around the bug may crash under ice cream sandwich:

org.xmlpull.v1.XmlPullParserException: expected: END_TAG {null}item (position:START_TAG <item>@1:37 in      at     at com.publicobject.waffles.Menu.parseXml( at com.publicobject.waffles.Menu.main(

The fix is to callnextTag()After a callnextText()Only if the current position is notEND_TAG:

  while (parser.nextTag() == XmlPullParser.START_TAG) {      parser.require(XmlPullParser.START_TAG, null, "item");      String itemText = parser.nextText();      if (parser.getEventType() != XmlPullParser.END_TAG) {          parser.nextTag();      }      parser.require(XmlPullParser.END_TAG, null, "item");      System.out.println("menu option: " + itemText);  }

The code above will parse XML correctly on all releases. If your application usesnextText()Extensively, use this helper method in place of callto

  private String safeNextText(XmlPullParser parser)          throws XmlPullParserException, IOException {      String result = parser.nextText();      if (parser.getEventType() != XmlPullParser.END_TAG) {          parser.nextTag();      }      return result;  }

Moving to a single xmlpullparser simplifies maintenance and allows us to spend more energy on improving system performance.

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.