Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is 'Push Approach' and 'Pull Approach' to parsing?

Under the push parsing approach, a push parser generates synchronous events as a document is parsed, and these events can be processed by an application using a callback handler model

This is the text given in the book Pro XML Development with Java about SAX 2.0.

As for StAX, the book says:

Under the pull approach, events are pulled from an XML document under the control of the application using the parser.

I want to ask, what is the meaning of the highlighted text ? An answer befitting a beginner is appreciated :)

like image 975
An SO User Avatar asked Apr 09 '13 06:04

An SO User


People also ask

What is pull parsing?

Streaming pull parsing refers to a programming model in which a client application calls methods on an XML parsing library when it needs to interact with an XML infoset; that is, the client only gets (pulls) XML data when it explicitly asks for it.

What is XML Pull parser?

In android, the XMLPullParser interface provides the functionality to parse the XML files in android applications. The XMLPullParser is a simple and efficient parser method to parse the XML data when compared to other parser methods such as DOM Parser and SAX Parser.


2 Answers

Basically, a push is when the parser says to some handler, "I have a foo, do something with it." A pull is when the handler says to the parser, "give me the next foo."

Push:

if (myChar == '(')     handler.handleOpenParen(); // push the open paren to the handler 

Pull:

Token token = parser.next(); // pull the next token from the parser 
like image 83
yshavit Avatar answered Sep 29 '22 20:09

yshavit


Push Parsers - Events are generated by the API in the form of callback functions like startDocument(), endDocument() and are beyond control of programmer. We as a programmer could handle the events but generation of events is beyond control.

Pull Parsers - Events are generated when we call some API. Example shown below. So we as programmer can decide when to generate events.

   int eventType = xmlr.getEventType(); while(xmlr.hasNext()){      eventType = xmlr.next();      //Get all "Book" elements as XMLEvent object      if(eventType == XMLStreamConstants.START_ELEMENT &&           xmlr.getLocalName().equals("Book")){         //get immutable XMLEvent         StartElement event = getXMLEvent(xmlr).asStartElement();         System.out.println("EVENT: " + event.toString());      } }  

, The client only gets (pulls) XML data when it explicitly asks for it.

With pull parsing, the client controls the application thread, and can call methods on the parser when needed. By contrast, with push processing, the parser controls the application thread, and the client can only accept invocations from the parser.

like image 36
Jaydeep Rajput Avatar answered Sep 29 '22 19:09

Jaydeep Rajput