Be careful with the XmlPullParser. netText () method

Source: Internet
Author: User
Using XmlPullParser on Android is an efficient and easy-to-maintain method for parsing XML. Android has two implementation classes in history: using XmlPullParser on Android is an efficient and easy-to-maintain method for parsing XML. Android has two implementation classes in history:

  • KXmlParser, via XmlPullParserFactory. newPullParser ().

  • ExpatPullParser, via Xml. newPullParser ().

An error occurred when calling nextText () to implement Xml. newPullParser (). nextText () does not always give priority to END_TAG execution.

Therefore, some applications may encounter bugs when calling next () or nextTag ();

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("
 "              + "
 
  "              + "  
  
   Waffles
  "              + "  
  
   Coffee
  "              + "
 "));  }

In android4.0, the Xml. newPullParser () returned KxmlParser class is changed and the ExpatPullParser class is deleted. This fixes the nextTag () bug.

Unfortunately, the applications that may crash at present are all earlier than android4.0, and the following is an error message.

org.xmlpull.v1.XmlPullParserException: expected: END_TAG {null}item (position:START_TAG 
 
  @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 solution is to use nextTag () only after nextText () is called. only when the current position is not END_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 above code can correctly parse all xml versions. if the application uses nextText (), use the following auxiliary method in the nextText () field.

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

Using a single XmlPullParse simplifies our maintenance and allows us to spend more time tuning system performance.

The above is the content of the XmlPullParser. netText () method. For more information, see The PHP Chinese website (www.php1.cn )!


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: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.