Android:DOM vs SAX vs XMLPullParser 解析?

发布于 2024-12-27 13:14:13 字数 136 浏览 1 评论 0原文

我正在使用 SAX 解析器解析 XML 文档。

我想知道使用 DOMSAX ParserXMLPullParser 哪个更好、更快。

I am parsing XML Document using SAX Parser.

I want to know which is better and faster to work with DOM, SAX Parser or XMLPullParser.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

昵称有卵用 2025-01-03 13:14:13

这取决于你在做什么,如果你有非常大的文件,那么你应该使用 SAX 解析器,因为它会触发事件并释放它们,内存中不会存储任何内容,并且使用 SAX 解析器你无法以随机方式访问元素是没有回头路了! ,但是 Dom 允许您访问 xml 文件的任何部分,因为它将整个文件/文档保存在内存中。希望这能回答你的问题。

如果你想知道哪个最快的解析器 Xerces 将是你能找到的最快的并且 SAX 解析器应该给出你比Dom表现得更好

it depends on what are you doing , if you have very large files then you should use SAX parser since it will fire events and releasing them ,nothing is stored in memory ,and using SAX parser you can't access element in a random way there is no going back ! , but Dom let you access any part of the xml file since it keeps the whole file/document in memory . hope this answer you question .

if you want to know which fastest parser Xerces is going to be the fastest you'll find and SAX parser should give you more performance than Dom

等风来 2025-01-03 13:14:13

SAX XML 解析器已可用于 Android SDK。

http://developer.android.com/reference/org/xml/sax /XMLReader.html

因此很容易访问。

The SAX XML Parser already available into the Android SDK.

http://developer.android.com/reference/org/xml/sax/XMLReader.html

so it is easy to access.

倾城花音 2025-01-03 13:14:13

对不同类型的解析器进行分类的一方面是它们是否需要预先将整个 XML 文档加载到内存中。基于文档对象模型 (DOM) 的解析器可以做到这一点:它们将 XML 文档解析为树结构,然后可以在内存中遍历该树结构以读取其内容。这允许您以任意顺序遍历文档,并产生一些可以在 DOM 之上使用的有用 API,例如 XPath,一种专门为从树中提取信息而设计的路径查询语言。单独使用 DOM 并没有多大好处,因为它的 API 很笨重,而且即使不需要,总是将所有内容读入内存也是昂贵的。因此,在大多数情况下,DOM 解析器并不是在 Android 上解析 XML 的最佳选择。

有些解析器不需要预先加载文档。这些解析器是基于流的,这意味着它们在处理 XML 文档的同时仍然从数据源(Web 或磁盘)读取它。这意味着
您不能像使用 DOM 那样随机访问 XML 树,因为没有维护文档的内部表示。流解析器可以进一步区分。有一些推送解析器,在流式传输文档时,会在遇到新元素时回调到您的应用程序。 SAX 解析器就属于此类。然后是拉解析器,它更像迭代器或游标:这里客户端必须显式请求要检索的下一个元素。

来源:Android 实践。

One aspect by which different kinds of parsers can be classified is whether they need to load the entire XML document into memory up front. Parsers based on the Document Object Model (DOM) do that: they parse XML documents into a tree structure, which can then be traversed in-memory to read its contents. This allows you to traverse a document in arbitrary order, and gives rise to some useful APIs that can be slapped on top of DOM, such as XPath, a path query language that has been specifically designed for extracting information from trees. Using DOM alone isn’t much of a benefit because its API is clunky and it’s expensive to always read everything into memory even if you don’t need to. Hence, DOM parsers are, in most cases, not the optimal choice to parse XML on Android.

There are class of parsers that don’t need to load a document up front. These parsers are stream-based, which means they process an XML document while still reading it from the data source (the Web or a disk). This implies
that you do not have random access to the XML tree as with DOM because no internal representation of the document is being maintained. Stream parsers can be further distinguished from each other. There are push parsers that, while streaming the document, will call back to your application when encountering a new element. SAX parsers, fall into this class. Then there are pull parsers, which are more like iterators or cursors: here the client must explicitly ask for the next element to be retrieved.

Source: Android in Practice.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文