选择正确的 IOS XML 解析器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4181690/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Choosing the right IOS XML parser
提问by endy
There are a million different XML parsers for the iPhone. I have a medium sized XML file that contains alot of duplicate tags (at different points in the hierarchy). I was thinking about TBXML but I was concerned about its lack of XPath support. For instance lets say my XML file looked like this.
iPhone 有上百万种不同的 XML 解析器。我有一个中等大小的 XML 文件,其中包含大量重复标签(在层次结构的不同点)。我在考虑 TBXML,但我担心它缺乏 XPath 支持。例如,假设我的 XML 文件看起来像这样。
<blog>
<author> foo1 </author>
<comments>
<comment>
<text>
<![CDATA[ HTML code that must be extracted ]]>
</text>
<author>foo2</author>
</comment>
<comment>
<text>
<![CDATA[ Here is another post ]]>
</text>
<author>foo1</author>
</comment>
</comments>
</blog>
Basically my requirements are that I need to be able to extract that cdata. And know whether it is the blog author a comment author.
基本上我的要求是我需要能够提取该 cdata。并且知道博客作者是否是评论作者。
回答by Daniel Amitay
Best comparison I've seen on the subject:
我在该主题上看到的最佳比较:
http://www.raywenderlich.com/553/how-to-chose-the-best-xml-parser-for-your-iphone-project
http://www.raywenderlich.com/553/how-to-chose-the-best-xml-parser-for-your-iphone-project
The difference between the slowest and fastest parsers in his case was a factor of 2. Depending on your feature requirements, there are definitely parsers that can cut your parsing time in half.
在他的案例中,最慢和最快的解析器之间的差异是 2 倍。根据您的功能需求,肯定有解析器可以将您的解析时间减少一半。
回答by ZaBlanc
Check out RaptureXML. It's not official XPath behavior, but it's pretty close. Your query would be:
查看RaptureXML。这不是官方的 XPath 行为,但非常接近。您的查询将是:
[rxml iterate:@"comments.comment" with: ^(RXMLElement *e) {
if ([[e child:@"author"].text isEqualToString:@"foo1"]) {
NSLog(@"Blog Text is %@.", e.text);
} else {
NSLog(@"Comment Text is %@.", e.text);
}
}];
UPDATE: RaptureXML now supports XPath!
更新:RaptureXML 现在支持 XPath!
回答by onmyway133
You should look at these libraries, in order :)
您应该按顺序查看这些库:)
- Ono, powered by libxml2
- XMLDictionary
- RaptureXML
- 小野,由 libxml2 提供支持
- XML字典
- RaptureXML
Ono has the the cool AFOnoResponseSerializerwhich can be integrated with AFNetworking
Ono 有很酷的AFOnoResponseSerializer,可以与 AFNetworking 集成
回答by tadija
I made this new simple and lightweight XML parser for iOS in Swift - AEXML
我在 Swift 中为 iOS 制作了这个新的简单轻量级的 XML 解析器 - AEXML
You can use it to read XML data like this:
您可以使用它来读取 XML 数据,如下所示:
let someValue = xmlDocument["element"]["child"]["anotherChild"].value
or you can use it to build XML string like this:
或者您可以使用它来构建这样的 XML 字符串:
let document = AEXMLDocument()
let element = document.addChild("element")
element.addChild("child")
document.xmlString // returns XML string of the entire document structure
I hope it will be useful for someone.
我希望它对某人有用。
回答by Shan
As you have requirement for medium sized XML
由于您需要中等大小的 XML
TinyXML could be an good choice for medium sized documents if you already have experience with the API and are comfortable with C as it ports quite easily over to the iPhone.
如果您已经有使用 API 的经验并且熟悉 C,因为它很容易移植到 iPhone,那么 TinyXML 可能是中等大小文档的不错选择。