Domanda

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 :)

È stato utile?

Soluzione

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

Altri suggerimenti

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.

push parsing: it is where the parser pushes the parsing events to the application, most possibly using callback methods. Application can process asynchronously after calling any parser methods, so that if parser takes time app is not stuck at that point. As soon as parsing is complete the parser, through its callback event will trigger the app so that app can further continue with the parsing result.

pull parsing: when the app pull the data along rather than waiting for the parsing events. App can pull data in one by one manner, according to its requirements. like in the StAX, app calls next() method iteretively to get the next construct in XML.

You would can say that within push parsing a parser initiates a data stream and within pull parsing a application intiates a data stream.

Therefore a pull parser model is much economical: your app gets data only when it needs that.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top