Difference between pull parsing and sax parsing: pullsax Parsing

Source: Internet
Author: User
Tags xml parser

Difference between pull parsing and sax parsing: pullsax Parsing

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 xmlPullParser = pullFactory. newPullParser ();  
    // Method 2: Use the Android. util. Xml Utility 
   XmlPullParser xmlPullParser = Xml. newPullParser ();

   XmlPullParser. setInput (InputStream inputStream, String inputEncoding)Parse the corresponding Input File

   XmlPullParser. getEventType ()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 ).


What is the difference between Dom and SAX in xml parsing?

SAX concepts
SAX is the abbreviation of Simple API for XML. It is not a standard officially proposed by W3C. It can be said that it is a "folk" fact standard. In fact, it is a product of community discussions. Even so, there is no less DOM than the application of SAX in XML, and almost all XML parser will support it.

Compared with DOM, SAX is a lightweight method. We know that when processing the DOM, We need to read the entire XML document, and then create a DOM tree in the memory to generate each Node object on the DOM tree. When the document is small, this will not cause any problems, but once the document is large, it will become quite time-consuming and laborious to process the DOM. In particular, its demand for memory will also multiply, making it uneconomical to use DOM in some applications (such as in the applet ). At this time, a better alternative solution is SAX.

SAX is conceptually different from DOM. First of all, unlike the DOM document driver, it is event-driven, that is, it does not need to read the entire document, and the document reading process is the parsing process of SAX. Event-driven is a program running method based on the callback mechanism. (If you have a clear understanding of the new proxy event model in Java, this mechanism will be easily understood)

When XMLReader accepts XML documents, the XML documents are parsed during reading. That is to say, the process of reading the documents and the process of parsing are performed at the same time, which is very different from the DOM. Before parsing, You need to register a ContentHandler with XMLReader, which is equivalent to an event listener. Many methods are defined in ContentHandler, such as startDocument (), which is customized During the parsing process, something that should be handled at the beginning of the document. When XMLReader reads the appropriate content, it will throw the corresponding event and delegate the event processing permission to ContentHandler, and call the corresponding method to respond.

What is the difference between Dom parsing and SAX parsing?

This is not an appropriate question. There are too many things involved. Let's take a look at the single dom sax Parsing Method separately.

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.