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

允我心安 提交于 2019-11-28 19:15:05

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

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.

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