問題描述
像 SAX 和 StAX 這樣的流式 xml 解析器比構建像 DOM 解析器這樣的樹結構的解析器更快,內存效率更高.SAX 是一個推送解析器,這意味著它是觀察者模式(也稱為偵聽器模式)的一個實例.SAX 首先出現,但隨后出現了 StAX - 一個拉式解析器,這意味著它基本上像迭代器一樣工作.
Streaming xml-parsers like SAX and StAX are faster and more memory efficient than parsers building a tree-structure like DOM-parsers. SAX is a push parser, meaning that it's an instance of the observer pattern (also called listener pattern). SAX was there first, but then came StAX - a pull parser, meaning that it basically works like an iterator.
您可以在任何地方找到為什么更喜歡 StAX 而不是 SAX 的原因,但通常歸結為:它更易于使用".
You can find reasons why to prefer StAX over SAX everywhere, but it usually boils down to: "it's easier to use".
在關于 JAXP 的 Java 教程中,StAX 被模糊地表示為 DOM 和 SAX 之間的中間:它比 SAX 更容易,比 DOM 更高效".但是,我從未發現任何跡象表明 StAX 會比 SAX 更慢或內存效率更低.
In the Java tutorial on JAXP StAX is vaguely presented as the middle between DOM and SAX: "it's easier than SAX and more efficient than DOM". However, I never found any clues that StAX would be slower or less memory efficient than SAX.
這一切讓我想知道:有什么理由選擇 SAX 而不是 StAX?
推薦答案
概括一下,我認為 StAX
可以和 SAX
一樣高效.隨著 StAX
的改進設計,我真的找不到任何首選 SAX
解析的情況,除非使用遺留代碼.
To generalize a bit, I think StAX
can be as efficient as SAX
. With the improved design of StAX
I can't really find any situation where SAX
parsing would be preferred, unless working with legacy code.
編輯:根據這個博客 Java SAX 與 StAX StAX
不提供模式驗證.
EDIT: According to this blog Java SAX vs. StAX StAX
offer no schema validation.
這篇關于我什么時候應該選擇 SAX 而不是 StAX?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!