XmlPullParser cannot parse END_DOCUMENT, XmlPullParserException: Unexpected token (position: TEXT, unexpectedtoken

Source: Internet
Author: User

XmlPullParser cannot parse END_DOCUMENT, XmlPullParserException: Unexpected token (position: TEXT, unexpectedtoken

Recently, I encountered a pitfall. I found a piece of code on the Internet to parse XML. The snippets are as follows:

         int eventType = xpp.getEventType();         while (eventType != XmlPullParser.END_DOCUMENT) {          if(eventType == XmlPullParser.START_DOCUMENT) {              System.out.println("Start document");          } else if(eventType == XmlPullParser.END_DOCUMENT) {              System.out.println("End document");          } else if(eventType == XmlPullParser.START_TAG) {              System.out.println("Start tag "+xpp.getName());          } else if(eventType == XmlPullParser.END_TAG) {              System.out.println("End tag "+xpp.getName());          } else if(eventType == XmlPullParser.TEXT) {              System.out.println("Text "+xpp.getText());          }          eventType = xpp.next();         }

However, when debugging, how does the program parse to the end of the XML file and call the XmlPullParser. next () function?

Android-org.xmlpull.v1.XmlPullParserException: Unexpected token (position: TEXT error, because at the end of the file, the next () function does not return end_et et, so Parser will continue to parse and report an error. The solution is to add the While LOOP judgment condition to the judgment at the end of the xml file. The Code is as follows:
final int depth = parser.getDepth();        int type = parser.next();while ((type != XmlPullParser.END_TAG || parser                .getDepth() > depth) && type != XmlPullParser.END_DOCUMENT) {...//next}


Related Article

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.