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