http://ads.buzzcity.net/adpage.php?partnerid=40096
分类: 嵌入式
2011-12-21 09:52:45
[This post is by 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 to nextText() didn’t always advance to the END_TAGas the documentation promised it would. As a consequence, some apps may be working around the bug with extra calls tonext() or nextTag():
public void par***ml(Reader reader)In Ice Cream Sandwich we changed Xml.newPullParser() to return a KxmlParser and deleted our ExpatPullParser class. This fixes the nextTag() 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)The fix is to call nextTag() after a call to nextText() only if the current position is not an END_TAG:
while (parser.nextTag() == XmlPullParser.START_TAG) {The code above will parse XML correctly on all releases. If your application uses nextText() extensively, use this helper method in place of calls tonextText():
private String safeNextText(XmlPullParser parser)Moving to a single XmlPullParser simplifies maintenance and allows us to spend more energy on improving system performance.