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

孤者浪人 提交于 2020-01-09 13:09:06

问题


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


回答1:


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



回答2:


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.




回答3:


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.



来源:https://stackoverflow.com/questions/15895124/what-is-push-approach-and-pull-approach-to-parsing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!