search
HomeBackend DevelopmentXML/RSS TutorialBe careful with the XmlPullParser.netText() method

Using XmlPullParser on Android is a highly efficient and easy-to-maintain method of parsing XML. Android has historically had two implementation classes that implement this interface:

  • KXmlParser, via XmlPullParserFactory.newPullParser().

  • ExpatPullParser, via Xml.newPullParser().

There is a bug in the implementation of Xml.newPullParser() calling nextText(), nextText() is not always executed prior to END_TAG as mentioned in the documentation.

Therefore, some applications may have bugs in additional calls to 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("<?xml version=&#39;1.0&#39;?>"  
            + "<menu>"  
            + "  <item>Waffles</item>"  
            + "  <item>Coffee</item>"  
            + "</menu>"));  
}

In android 4.0, Xml.newPullParser() has been changed to return the KxmlParser class, and at the same time Removed ExpatPullParser class. This fixes the nextTag() bug.

Unfortunately, the current applications that may crash are all versions lower than android 4.0. The following is the error message.

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 solution is to jump to nextTag() only after calling nextText(), 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() extensively, use the following auxiliary method where nextText() is used.

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 energy on improving system performance.

The above is the content of the Be careful with the XmlPullParser.netText() method. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!


Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
RSS Feeds: Exploring XML's Role and PurposeRSS Feeds: Exploring XML's Role and PurposeApr 28, 2025 am 12:06 AM

XML's role in RSSFeed is to structure data, standardize and provide scalability. 1.XML makes RSSFeed data structured, making it easy to parse and process. 2.XML provides a standardized way to define the format of RSSFeed. 3.XML scalability allows RSSFeed to add new tags and attributes as needed.

Scaling XML/RSS Processing: Performance Optimization TechniquesScaling XML/RSS Processing: Performance Optimization TechniquesApr 27, 2025 am 12:28 AM

When processing XML and RSS data, you can optimize performance through the following steps: 1) Use efficient parsers such as lxml to improve parsing speed; 2) Use SAX parsers to reduce memory usage; 3) Use XPath expressions to improve data extraction efficiency; 4) implement multi-process parallel processing to improve processing speed.

RSS Document Formats: Exploring RSS 2.0 and BeyondRSS Document Formats: Exploring RSS 2.0 and BeyondApr 26, 2025 am 12:22 AM

RSS2.0 is an open standard that allows content publishers to distribute content in a structured way. It contains rich metadata such as titles, links, descriptions, release dates, etc., allowing subscribers to quickly browse and access content. The advantages of RSS2.0 are its simplicity and scalability. For example, it allows custom elements, which means developers can add additional information based on their needs, such as authors, categories, etc.

Understanding RSS: An XML PerspectiveUnderstanding RSS: An XML PerspectiveApr 25, 2025 am 12:14 AM

RSS is an XML-based format used to publish frequently updated content. 1. RSSfeed organizes information through XML structure, including title, link, description, etc. 2. Creating RSSfeed requires writing in XML structure, adding metadata such as language and release date. 3. Advanced usage can include multimedia files and classified information. 4. Use XML verification tools during debugging to ensure that the required elements exist and are encoded correctly. 5. Optimizing RSSfeed can be achieved by paging, caching and keeping the structure simple. By understanding and applying this knowledge, content can be effectively managed and distributed.

RSS in XML: Decoding Tags, Attributes, and StructureRSS in XML: Decoding Tags, Attributes, and StructureApr 24, 2025 am 12:09 AM

RSS is an XML-based format used to publish and subscribe to content. The XML structure of an RSS file includes a root element, an element, and multiple elements, each representing a content entry. Read and parse RSS files through XML parser, and users can subscribe and get the latest content.

XML's Advantages in RSS: A Technical Deep DiveXML's Advantages in RSS: A Technical Deep DiveApr 23, 2025 am 12:02 AM

XML has the advantages of structured data, scalability, cross-platform compatibility and parsing verification in RSS. 1) Structured data ensures consistency and reliability of content; 2) Scalability allows the addition of custom tags to suit content needs; 3) Cross-platform compatibility makes it work seamlessly on different devices; 4) Analytical and verification tools ensure the quality and integrity of the feed.

RSS in XML: Unveiling the Core of Content SyndicationRSS in XML: Unveiling the Core of Content SyndicationApr 22, 2025 am 12:08 AM

The implementation of RSS in XML is to organize content through a structured XML format. 1) RSS uses XML as the data exchange format, including elements such as channel information and project list. 2) When generating RSS files, content must be organized according to specifications and published to the server for subscription. 3) RSS files can be subscribed through a reader or plug-in to automatically update the content.

Beyond the Basics: Advanced RSS Document FeaturesBeyond the Basics: Advanced RSS Document FeaturesApr 21, 2025 am 12:03 AM

Advanced features of RSS include content namespaces, extension modules, and conditional subscriptions. 1) Content namespace extends RSS functionality, 2) Extended modules such as DublinCore or iTunes to add metadata, 3) Conditional subscription filters entries based on specific conditions. These functions are implemented by adding XML elements and attributes to improve information acquisition efficiency.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Atom editor mac version download

Atom editor mac version download

The most popular open source editor