iPhone 开发 - XMLParser 与 libxml2 与 TouchXML
我找不到这些解析技术的比较。 哪一种最常用?
问候。 穆斯塔法
I cannot find comparison of these parsing technique. Which one is most commonly used?
Regards.
Mustafa
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
NSXMLParser 是一个 SAX 解析器,这意味着它会逐步遍历 XML 文档,并通知您(通过委托方法) )各种事件(例如启动 xml 节点、查找属性等)。 这种类型的 XML 处理最适合解析大型文档,以及当您只需要从大文件中检索少量数据时。
与 SAX 相反的是 DOM 模型,其中整个 XML 树被解析到内存中(通常使用一次调用),然后您可以从提供的 API 中自由探索 XML 文档。 这通常更容易使用,因为您可以随机访问整个 xml 文档。
因此,应该回答的第一个问题是 SAX 与 DOM 问题。 如果您正在处理大型 XML 文件(例如 10 MB 或更大),您可能需要坚持使用 SAX。 如果您正在处理小型 XML 文件或小型 XML 片段,那么使用 DOM 通常会更容易。
如果您确实决定使用 DOM,那么您有几个选择。
libxml2 是一个用 C 语言编写的非常强大的 API。它主要用于 DOM 样式的使用,但如果您对它有经验的话,还有其他选项。 但是,由于它是用 C 编写的,因此对于那些更熟悉 Objective-C(对象和自动释放)或来自 .Net 世界的人来说,它通常没有吸引力。 因此,需要/渴望有一个围绕 libxml 的原生 Objective-C 包装器,以使其更熟悉。 TouchXML 就是这样的一种包装器。 如果您只需要对 XML 进行读取访问,那么它会很有效。 如果您想更改 XML,或从头开始创建 XML 片段/文档,我建议使用 KissXML 。
NSXMLParser is a SAX parser, meaning it steps through the XML document, and informs you (via delegate methods) of various events (such as starting an xml node, finding attributes, etc). This type of XML processing is best for parsing huge documents, and when you only need to retrieve a tiny amount of data from a big file.
In contrast to SAX is the DOM model, where the entire XML tree is parsed into memory (usually with a single call), and then you can freely explore the XML document from the presented API. This is generally much easier to work with, as you get random access to the entire xml document.
So the first question one should answer is the SAX vs DOM question. If you're woring with big XML files (say 10 MB or bigger), you may want to stick with SAX. If you're working with small XML files, or tiny XML fragments, it's often much easier to use DOM.
If indeed you decide to go with DOM, you've got a few options.
libxml2 is a very powerful API written in C. It's mainly for DOM style use, but has other options if you become experienced with it. But, as it's written in C, it's not often attractive to those more familiar with Objective-C (objects, and autorelease), or those coming over from the .Net world. Thus, there is a need/desire for a native Objective-C wrapper around libxml to make it more familiar. TouchXML is one such wrapper. It works well if you only need read access to the XML. If you want to alter the XML, or create XML fragments/documents from scratch, I'd recommend going with KissXML.
请参阅 XML 教程iOS:如何为您的 iPhone 项目选择最佳的 XML 解析器:
See XML Tutorial for iOS: How To Choose The Best XML Parser for Your iPhone Project:
还有另一个很好的基于块的 XML 解析库 - RaptureXML (github),作者:ZaBlanc,它在 libxml2 之上提供 Objective-C API,我绝对推荐尝试一下。
There is another good block-based XML parsing library - RaptureXML (github) by ZaBlanc , it provide Objective-C API on top of libxml2, I definitely recommend to try it.