Differences between pull parsing and sax Parsing

Source: Internet
Author: User

If we only need the first part of data in an XML document, but the whole document will be parsed using the sax or DOM method, although we do not need to parse most of the data behind the XML document, this actually wastes processing resources. It is appropriate to use the pull method.

The pull parser and the SAX Parser have differences but similarities. The difference is that the SAX Parser automatically pushes events to the registered event processor for processing, so you cannot control the event processing to take the initiative to end; the pull parser is used to allow your application code to actively retrieve events from the parser, because it is actively obtaining events, therefore, after meeting the required conditions, you can stop obtaining the event and complete the parsing. This is their main difference.

While their similarity is in the running mode, the pull parser also provides events similar to sax, including start_document and end_document, start_tag, and end_tag, when encountering element content such as text, you need to call the next () method to extract them (actively extract events ).

In the Android system, the package related to the PULL mode is Org. xmlpull. v1: The factory class xmlpullparserfactory and pull parser of the pull parser are provided in this package. The xmlpullparserfactory instance calls the newpullparser method to create an xmlpullparser parser instance, then, the xmlpullparser instance can call methods such as geteventtype () and next () to actively extract events and perform logical Processing Based on the extracted event types.

// Create xmlpullparser in two ways
// Method 1: Use the factory class xmlpullparserfactory
Xmlpullparserfactory pullfactory = xmlpullparserfactory. newinstance ();
Xmlpullparser = pullfactory. newpullparser ();
// Method 2: Use the Android. util. xml Utility
Xmlpullparser = xml. newpullparser ();

Xmlpullparser. setinput (inputstream, string inputencoding) parses the corresponding Input File

Xmlpullparser. geteventtype () to obtain the element

Determine whether the element is the one in start_document, end_document, start_tag, end_tag, and text and parse it accordingly.
The pull method is relatively simple, and can be used to stop parsing Based on judgment (DOM and sax both need to not stop parsing the file in the middle of the article ).

Differences between pull parsing and sax Parsing

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.