在iPhone上进行XML解析的最佳方法 [英] Best approach for XML parsing on the iPhone

查看:118
本文介绍了在iPhone上进行XML解析的最佳方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经熟悉了iPhone SDK中的NSXMLParser,但我发现它的事件驱动性质对我来说很尴尬。我只是想提取一些元素值,但是这个必须处理startElement,foundCharacters和endElement的概念似乎比实际应该更多的工作。我只是以错误的方式看待这个问题,还是在iPhone SDK中使用更简单的基于树/ DOM的XML方式?

I've familiarized myself with the NSXMLParser from the iPhone SDK but I find the event-driven nature of it awkward for my purposes. I just want to extract some element values but this concept of having to handle the startElement, foundCharacters, and endElement seems like more work than it really should be. Am I just looking at this the wrong way or is there a simpler tree/DOM-based way of working with XML in the iPhone SDK?

如果建议只是使用NSXMLParser,我是否可以使用某些设计模式来保持我的代码在startElement方法中具有5级嵌套ifs?

If the advice is to just work with NSXMLParser, are there certain design patterns I can use to keep my code from having 5 levels of nested ifs in the startElement method?

推荐答案


如果你在iPhone上,使用基于树的解析可能是一个令人望而却步的内存耗尽。相信我,我一直在那里,并且在我的主要iPhone应用程序开发的最后五个月里,我尝试了许多不同的方法。基于树的解析工作正常,直到您下载包含400条非常长的注释的某人​​的评论流,大约600KB的原始数据。除了生成的XML树的大小之外,在创建该树时内部分配的内存可能是巨大的。

If you're on the iPhone, using tree-based parsing can be a prohibitive memory hog. Trust me, I've been there, and I've tried many different approaches over the last five months of development of my main iPhone application. Tree-based parsing works fine until you download someone's comment stream containing 400 very long comments, clocking in at about 600KB of raw data. Quite aside from the size of the resultant XML tree, the memory allocated internally while creating that tree can be enormous.


我创建了一个NSXMLParser变体,它从提供的NSInputStream中提取数据而不是使用单个数据块,一次只传递1KB到libxml进行处理(NSXMLParser也使用libxml,但一次传递100%的数据)。

I wound up creating a variant of NSXMLParser which pulls data in from a supplied NSInputStream rather than using a single chunk of data, and which passes only 1KB at a time into libxml for handling (NSXMLParser uses libxml too, but passes 100% of the data in one go).


源代码可用(查看StreamingXMLParser文件夹)。你还会在那里找到一个代表超类;对于大多数解析需求,您可以继承AQXMLParserDelegate并实现 -start [Element] WithAttributes:(NSDictionary *)attrs -end [Element] 。这些方法将在发现起始和结束标记时为您调用,在结束标记内,您可以使用 self.characters 来访问元素的内容字符或CDATA。

The source code is available on github (look in the StreamingXMLParser folder). You'll also find a delegate superclass in there; for most parsing needs you can subclass AQXMLParserDelegate and implement -start[Element]WithAttributes: (NSDictionary *) attrs and -end[Element] in your subclass. These methods will be called for you as start & end tags are discovered, and inside the end tag you can use self.characters to access the content characters or CDATA of the element.


有关不同解析器的相对内存占用量的更多信息(尽管在Mac上,而不是iPhone上),请参阅我原来的博文< a href =http://alanquatermain.net/post/93651539/aqxmlparser-big-memory-win =nofollow noreferrer>这里
和NSXMLDocument的后续工作这里

For more on the relative memory footprints of the different parsers (albeit on the Mac, not the iPhone) see my original blog post here and the followup on NSXMLDocument here.

这篇关于在iPhone上进行XML解析的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆