SAX 解析器与 XMLPull 解析器
我了解 SAX 解析器与 XMLPull 解析器的工作方式之间的区别。 事实上,这里有一个很好的解释:
http:// www.firstobject.com/xml-reader-sax-vs-xml-pull-parser.htm 这篇文章有点以 .NET 为中心,但概念是适用的。
虽然我同意作者的观点,即 Pull 解析器更容易使用,但我很困惑哪种类型的解析器在哪种情况下会更好。 如果有人能阐明并指出我更多的阅读内容,我将不胜感激。
谢谢。
I understand the difference between how the SAX parser works vs the XMLPull parser.
In fact there's a pretty good explanation here:
http://www.firstobject.com/xml-reader-sax-vs-xml-pull-parser.htm
The article is a bit .NET centric but the concepts apply.
While I agree with the author's opinion that the Pull parser is easier to work with, I'm pretty confused as to which type of parser would be better in which situations.
If anyone could shed any light and point me to some more reading I would appreciate it.
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
我发现他们俩都很烂。 (我有一个更好的解决方案建议)
您应该使用基于简单注释的 XML 库。我喜欢它并将其用于我的所有项目。如果您阅读本教程,那么我想您会发现它将能够用更少的代码更快地完成您想要的一切。 (因此不易出现错误)在内部,该库使用您要求的那些解析器来完成繁重的工作。
然后,您可以阅读我的博客文章,将其包含在如果需要的话,可以选择一个 Android 项目。 (它适用于至少 1.5 版以上的所有 Android 版本,这意味着基本上适用于所有人)
I find that they both suck. (And I have a better solution to suggest)
You should use the Simple annotation based XML library. I love it and use it for all of my projects. If you read through the tutorial then I think you will find that it will be able to do everything that you want and much faster and with less code. (Thus being less bug prone) Internally the library uses those parsers that you were asking about to do the heavy lifting.
You can then read my blog post on including it in an Android project if you want. (It will work in every version of Android from atleast 1.5 up which means for everybody basically)
这完全取决于情况,例如,如果 xml 文件确实很大,那么您就不能选择 DOM 解析器,因为它们会首先将文件放入内存,然后对其进行解析,我发现解析大小为 n 的文件需要7n内存空间。在这种情况下,您应该选择 SAX 解析器,它轻便并且消耗更少的内存。
第二种情况是当文件不是很大时,在这种情况下您可以使用 XML pull 解析器,因为在这种情况下您将完全控制 xml,您可以跳过 SAX 中不可能的任何解析周期。因此,如果您要查找的标签是文件中的第一个标签,那么为什么要查找整个文件。
据我所知,如果您只考虑小文件的速度,请使用 XML pull 解析器,如果文件很大并且您想解析全部内容,请使用 SAX。
It totally depends on the situation for e.g If the xml file is really large than you can't opt for DOM parsers as they will first bring the file in to memory and then it will be parsed and i found that parsing a file of size n requires 7n memory space. In this case you should opt for SAX parser its light and will consume less memory.
Second case is when the file is not really large, in this case you can go for XML pull parser because in this you will have full control on the xml you can skip the parsing cycle any where that is not possible in SAX. So if the tag you are looking for is the first one in the file then why would you go for whole file.
So as far as i know if you consider only speed with small file go with XML pull parser and If the file is large and you want to parse it all then go with SAX.
两个解析器在内存/时间方面基本上是相同的。
唯一的事情是,使用拉解析器,您可以拉出诸如 startelement 和 endelement 之类的事件,并且只关注您想要的事件。
与 android sax 解析器一样,您别无选择,只需将代码放在您想要的位置,但必须包含所有事件。
此处是一个链接,您可以参考该链接进行进一步阅读。
Both the parsers are basically the same memory/time wise.
The only thing being that with pull parser you can pull out the events like startelement and endelement and only heed the ones that you want to.
where as with android sax parsers, you have no choice, you just put code where you want to but you have to include all the events.
here is a link which you can refer for further reading.
我发现 SAX 模型在一种特定情况下更容易使用:您将使用自定义数据结构构建整个文档(或至少其主要部分)的自己的内存中表示。 (如果您不特别关注数据结构,那么 DOM 解析器已经做到了这一点。)
I find the SAX model easier to work with in one specific situation: where you are going to build your own in-memory representation of the entire document (or at least major portions of it) with custom data structures. (If you aren't particular about the data structure, then the DOM parser already does this.)
Pull 和 Sax 的相似之处在于,它们都是低级流方法,比 DOM 更快、内存效率更高,但 Pull 比 SAX 有一些优势:
Pull 比 SAX 更容易实现,因为您不必维护解析器的状态(使用附加变量能够知道解析器当前位于 XML 树中的位置)。 Pull 解析器代码中的嵌套循环或多或少会与文档的 XML 层次结构相匹配,因此我认为 Pull 解析器代码也比 SAX 解析器代码更具可读性。
使用拉解析器代码,您可以跳过不想解析的整个块,因此它也比 SAX 更有效,SAX 总是提取所有节点的主要信息。使用拉式解析器,如果您获取了所需的信息,您还可以随时停止解析,而这对于 SAX 来说是不可能的。
此外,您还可以使用拉解析器来实现 SAX 解析器。相反的情况是不可能的。
出于所有这些原因,我相信拉解析器在所有情况下都优于 SAX,但是与 SAX 一样,正确实现它并不容易,您必须小心。如果您不需要 pull 和 SAX 的低级速度优势并且您的 XML 是干净的,那么您始终可以使用更高级别的解析库,例如 简单为您完成艰苦的工作。
Pull and Sax are similar in the way that they are both low-level streaming approaches which are faster and more memory efficient than DOM, but pull has a few advantages over SAX:
Pull is easier to implement than SAX because you don't have to maintain the state of your parser (using additional variables to be able to know in which place your parser currently is in the XML tree). The nested loops in your pull parser code will more or less match the XML hierarchy of your document so I think the Pull parser code is also more readable than the SAX parser code.
With pull parser code you can skip entire blocks that you don't want to parse so it's also more efficient than SAX which always extracts the main information of all nodes. Using a pull parser, you can also stop the parsing at any moment if you fetched the information you wanted, which is not possible with SAX.
Also, you can implement a SAX parser using a pull parser. The opposite is not possible.
For all these reasons I believe the pull parser is superior to SAX in all situations, however like SAX it's not trivial to implement properly and you have to be careful. If you don't need the low-level speed benefits of pull and SAX and your XML is clean, you can always use a higher-level parsing library like Simple to do the hard work for you.
我在使用 SAX 而不是 XMLPullParser 时发现了更好、更高效的输出...我的场景是解析 XML 标签下的属性,我可以轻松完成并将其顺利插入数据库...我认为这取决于情况,当我需要在 XML 文件上写入,我更喜欢 DOM 解析器...
I found better and more efficient output while using SAX rather than XMLPullParser... My scenario is to parse the attributes under a XML tag, i could do it easily and insert it into Database smoothly... I think it depends on situations, when i need to write on a XML file i prefer DOM Parser...
我建议使用 XmlPullParser 一个.. Sax 解析器没有从我的测试中的提要中检索标签.. xmlpullparser 很容易做到这一点 =)
也取决于你的喜好
i'd recommend using the XmlPullParser one.. the Sax parser didn't retrieve the tag from a feed in my test.. xmlpullparser did that easily =)
also depends on your preferences also