注意Android 的 XmlPullParser.nextText()

來源:互聯網
上載者: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]

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

  • KXmlParser, via
    XmlPullParserFactory.newPullParser().

  • ExpatPullParser, via
    Xml.newPullParser().

The implementation from Xml.newPullParser() had a bug where calls tonextText() didn’t always advance to the
END_TAG as the documentation promised it would. As a consequence, some apps may be working around the bug with extra calls tonext() or
nextTag():

    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 changed Xml.newPullParser() to return a KxmlParser and deleted our ExpatPullParser class. This fixes thenextTag() 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 java.io.StringReader@40442fa8)      at org.kxml2.io.KXmlParser.require(KXmlParser.java:2046)     at com.publicobject.waffles.Menu.parseXml(Menu.java:25) at com.publicobject.waffles.Menu.main(Menu.java:32)

The fix is to call nextTag() after a call to nextText() only if the current position is not anEND_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 calls to
nextText():

  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.

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.