java 什么是解析的“推方法”和“拉方法”?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15895124/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 21:12:55  来源:igfitidea点击:

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

javaxmlsaxparserstax

提问by Little Child

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 Javaabout SAX 2.0.

这是Pro XML Development with Java关于 SAX 2.0一书中给出的文本。

As for StAX, the book says:

至于 StAX,书中说:

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

在拉取方法下,事件是从使用解析器的应用程序控制下的 XML 文档中拉取的。

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

我想问一下,突出显示的文字是什么意思?适合初学者的答案表示赞赏:)

回答by yshavit

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."

基本上,推送是解析器对某个处理程序说:“我有一个 foo,用它做点什么。” 拉是指处理程序对解析器说“给我下一个 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

回答by JRR

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.

推送解析器- 事件由 API 以回调函数的形式生成,例如 startDocument()、endDocument(),并且不受程序员的控制。我们作为程序员可以处理事件,但事件的生成是无法控制的。

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

Pull Parsers- 当我们调用一些 API 时会生成事件。示例如下所示。所以我们作为程序员可以决定什么时候生成事件。

   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.

, 客户端仅在显式请求时才获取(拉取)XML 数据。

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.

通过拉式解析,客户端控制应用程序线程,并可以在需要时调用解析器上的方法。相比之下,在推送处理中,解析器控制应用程序线程,客户端只能接受来自解析器的调用。

回答by Ankit

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.

拉解析:当应用程序拉数据而不是等待解析事件时。App可以根据自己的需求,一一拉取数据。就像在 StAX 中一样,应用程序反复调用 next() 方法以获取 XML 中的下一个构造。