- 浏览: 1031345 次
- 性别:
- 来自: 上海
文章分类
- 全部博客 (675)
- ios (214)
- android-course (5)
- unity3d (7)
- cocos2d (36)
- html5 (3)
- game (5)
- android (42)
- java (57)
- php (12)
- 创业 (10)
- SEO (3)
- 架构 (2)
- 数据库 (3)
- 产品设计 (9)
- 操作系统 (10)
- Web前端 (11)
- 其他 (50)
- GAE (1)
- mac os (8)
- Open Source (2)
- 序列号 (10)
- C (2)
- database (2)
- 算法 (6)
- 设计模式 (1)
- photoshop (1)
- 3dmax (1)
- maya (1)
- opengl (3)
- 游戏设计 (1)
- 趋势 (1)
- cocos2d-x (4)
- shell (3)
- c++ (30)
- lua (5)
- flash (1)
- spring (3)
- mysql (4)
- Git (6)
- xmpp (1)
- cocos2dx (14)
- mac (2)
- 编程规范 (2)
- windows (1)
- linux (5)
- coocs2dx (1)
- ubuntu (2)
- aws (1)
- OPENGLES (1)
- 原画 (1)
最新评论
-
jlees:
Best mobile app testing tool pc ...
iOS + XCode 4 + GHUnit = Mobile TDD+Continuous testing -
ipanda:
楼主,能否给一个Micro CloudFoundry的虚机或者 ...
Cloud Foundry使用及开发向导 -
love_zongming:
谢谢分享。。
visio2007序列号 -
雨花台舞水:
你这才是枪文把
套在 360 黑匣子外面的黑盒子:你被技术型枪稿吓到了么? -
hugh.wang:
改天试试
Mac版魔兽争霸3 1.24e下载
There are a lot of options when it comes to parsing XML on the iPhone. The iPhone SDK comes with two different libraries to choose from, and there are several popular third party libraries available such as TBXML, TouchXML, KissXML, TinyXML, and GDataXML. How is a developer to choose?
I have been recently taking a look at the various options out there, and ended up extending the XMLPerformance sample from Apple to try out each of the above libraries to learn how they worked and compare their performance. I thought I’d share what I’ve learned thus far to others who might be searching for the best XML library for their iPhone project.
In this article we’ll give a detailed comparison of the features and performance of the most popular iPhone libraries, explain how to choose between them, and give a sample project showing how to read XML data using each of the above libraries.
SAX vs. DOM
Before we begin, I wanted to make sure everyone is aware of the most important difference between XML parsers: whether the parser is a SAX or a DOM parser.
- A SAX parser is one where your code is notified as the parser walks through the XML tree, and you are responsible for keeping track of state and constructing any objects you might want to keep track of the data as the parser marches through.
- A A DOM parser reads the entire document and builds up an in-memory representation that you can query for different elements. Often, you can even construct XPath queries to pull out particular pieces.
Ok, now let’s discuss some of the libraries!
The Most Popular XML Parsers for the iPhone
In my research, here’s what seemed to me to be the most popular XML Parsers for the iPhone, and a brief description of each one:
- NSXMLParser is a SAX parser included by default with the iPhone SDK. It’s written in Objective-C and is quite straightforward to use, but perhaps not quite as easy as the DOM model.
- libxml2 is an Open Source library that is included by default with the iPhone SDK. It is a C-based API, so is a bit more work to use than NSXML. The library supports both DOM and SAX processing. The libxml2 SAX processor is especially cool, as it has a unique feature of being able to parse the data as it’s being read. For example, you could be reading a large XML document from the network and displaying data that you’re reading for it to the user while you’re still downloading.
- TBXML is a lightweight DOM XML parser designed to be as quick as possible while consuming few memory resources. It saves time by not performing validation, not supporting XPath, and by being read-only – i.e. you can read XML with it, but you can’t then modify the XML and write it back out again.
- TouchXML is an NSXML style DOM XML parser for the iPhone. Like TBXML, it is also read-only, but unlike TBXML it does support XPath.
- KissXML is another NSSXML style DOM XML parser for the iPhone, actually based on TouchXML. The main difference is KissXML also supports editing and writing XML as well as reading.
- TinyXML is a small C-based DOM XML parser that consists of just four C files and two headers. It supports both reading and writing XML documents, but it does not support XPath on its own. However, you can use a related library – TinyXPath – for that.
- GDataXML is yet another NSXML style DOM XML parser for the iPhone, developed by Google as part of their Objective-C client library. Consisting of just a M file and a header, it supports both reading and writing XML documents and XPath queries.
Ok, now let’s start comparing all these libraries!
XML Parser Performance Comparison App
Apple has made an excellent code sample called XMLPerformance that allows you to compare the time it takes to parse a ~900KB XML document containing the top 300 iTunes songs with both the NSXML and libxml2 APIs.
The sample allows you to choose a parsing method and then parse the document, and it keeps statistics on how long it took to download the file and parse the file in a database. You can then go to a statistics screen to see the average download and parse times for each method.
I thought this would be an ideal way to test out how these various APIs performed against each other, so I extended the sample to include all of the above libraries. You can download the updated project below if you want to try it out on your device. It also serves as a nice example of how to use each of the above APIs!
Download Updated XMLPerformance Project
A note on the project: if the library included XPath support, I used it for a single lookup, because I felt it represented the way the library would be used in practice. But of course XPath is generally slower than manually walking through the tree, so it adds to the benchmarks for those libraries.
So anyway – I’ll discuss the results of how things performed on my device here with the sample written as-is – but feel free to give it a shot on your device, or tweak the code based on the actual XML data you need to parse!
XML Parser Performance Comparison
Here’s some graphs that shows how quickly the various parsers parsed the XML document on my device (an iPhone 3Gs):
As you can see here, NSXMLParser was the slowest method by far. TBXML was the fastest, which makes sense because many features were taken out in order to optimize parse time for reading only.
I was surprised, however, to see that TBXML and some of the other DOM parsing methods performed faster than libxml2′s SAX parser, which I had thought would be the fastest of all of the methods. I haven’t profiled it, but my guess as to why it is slower is because of the frequent string compares needed to parse the document in the SAX method.
However, don’t discount libxml2′s SAX method by looking at this chart. Remember that libxml2 is the only one of these methods that can parse the document as it’s reading in – so it can let your app start displaying data right away rather than having to let the download finish first.
Ok, here’s a graph that shows the peak memory usage by parser (this was obtained through running the various methods through the Object Allocations tool):
Note that the DOM methods usually require more memory overhead than the SAX methods (with the exception of TBXML, which is indeed quite efficient). This is something to consider when you are dealing with especially large documents, given the memory constraints on an iPhone.
Also note that libxml2′s SAX method is the best option as far as peak memory usage is concerned (and I suspect it would scale better than the others as well).
Finally, let’s wrap up with a chart that summarizes the differences between the parsers and everything we’ve discussed above:
Included with SDK? | Yes | Yes | No | No | No | No | No | Yes |
Seconds to Parse | 1.87 | 1.19 | 0.68 | 1.1 | 1.37 | 1.27 | 1.07 | 0.84 |
Peak Memory Usage | 3.11 | 3.01 | 3.07 | 6.5 | 5.25 | 4.8 | 4.15 | 4.97 |
Parse While Downloading? | No | Yes | No | No | No | No | No | No |
Edit/Save XML? | No | No | No | No | Yes | Yes | Yes | Yes |
XPath Support? | No | No | No | Yes | Yes | Yes* | Yes | Yes |
C or Obj-C | Obj-C | C | Obj-C | Obj-C | Obj-C | C | Obj-C | C |
License | Apple | MIT | MIT | MIT | MIT | ZLib | Apache | MIT |
* = with TinyXPath
Which To Choose?
Which XML parser to choose really depends on what you want to do with the parser.
- If you just want to read small XML documents, performance doesn’t matter as much with small documents. You probably want to pick something with XPath support and something that is written in Objective-C to make your job easier. So I’d recommend either TouchXML, KissXML, or GDataXML for this case.
- If you want to both read and write small XML documents, again performance doesn’t matter as much as functionality and ease of use. You probably want to pick something with XPath support, written in Objective-C, with read/write capability. So I’d recommend KissXML or GDataXML for this case.
- If you want to read extremely large XML documents, performance is the critical issue here. You’ll want to consider libxml2 SAX, TBXML, or libxml DOM for this, depending on what your exact situation is.
What about the ones I didn’t mention?
- NSXML is a decent choice if you’re dealing with relatively small documents, and you don’t feel like adding a third party library to the SDK.
- TinyXML could be an OK 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.
I took a look at two other XML libraries during the course of this investigation (VTD-XML and Objective-XML), but I couldn’t get them working. If someone else has had more luck with these, feel free to extend the sample project to include them!
Where To Go From Here?
If you’re looking for some help using one of these libraries, check out my post on How to Read and Write XML Documents with GDataXML.
And if anyone has any additional feedback about these libraries or tips that may help other developers, please chime in below!
转载自:http://www.raywenderlich.com/553/how-to-chose-the-best-xml-parser-for-your-iphone-project
发表评论
-
Mac上安装Protocol Buffers
2016-09-18 11:29 8191.下载文件 (http://code.google.com ... -
webview点击获取图片
2016-04-01 17:12 827UILongPressGestureRecognizer * ... -
hexo 自动部署脚步
2016-03-29 21:17 932echo "===============star ... -
自定义navigationItem.leftBarButtonItem后,系统默认的手势滑动失效解决方案
2016-03-01 18:01 1280自定义navigationItem.le ... -
UITextView autolayout 高度自适应
2016-02-15 23:26 1413UITextView *t = [[UITextView ... -
腾讯敏捷框架TAPD》研究
2015-11-19 20:47 1420这篇文档是研究心得 ... -
ios image 压缩
2015-11-06 12:09 837- (UIImage *)_scaleToSize:(UII ... -
iphone分辨率图解
2015-11-04 17:33 565iphone分辨率图解 -
IOS中获取各种文件的目录路径的方法
2015-09-24 12:10 647iphone沙箱模型的有四个文件夹,分别是什么,永久数据存储 ... -
Customizing Navigation Bar and Status Bar in iOS 7
2015-08-17 20:23 1606Like many of you, I have been ... -
GCD 深入理解:第一部分
2015-07-24 14:49 767本文翻译自 http://www.raywenderlich ... -
Mac上的抓包工具Charles
2015-05-06 01:09 5316Mac上的抓包工具Charles 分类: IO ... -
如何移除发布版本中的NSLog输出
2015-05-04 20:27 749Phone开发中会经常使用NSLog将一些运行信息输出到终端 ... -
xcode4的环境变量,Build Settings参数,workspace及联编设置
2015-03-27 11:23 925一、xcode4中的环境变量 $(BUILT_PROD ... -
数字签名是什么?
2014-11-25 16:58 616http://www.ruanyifeng.com/blog/ ... -
让你的Xcode更加高效
2014-10-29 00:16 518http://www.tairan.com/archives/ ... -
我所经历的“余额宝”的那些故事
2014-06-08 01:05 758“余额宝”经过不到 ... -
代码手写UI,xib和StoryBoard间的博弈,以及Interface Builder的一些小技巧
2014-05-31 01:25 794最近接触了几个刚入门的iOS学习者,他们之中存在一个普遍 ... -
WWDC 2013 Session笔记 - iOS7中的多任务
2014-05-31 01:24 661这是我的WWDC2013系列笔记中的一篇,完整的笔记列表 ... -
APP被苹果App Store拒绝的79个原因(未完待续)
2014-05-09 10:49 1148作为iOS开发者,估计有很多都遇到过APP提交到App Sto ...
相关推荐
XMLParser在iOS开发中是一种常用的解析XML数据的工具,它允许开发者将XML文件转换为可操作的数据结构,便于在iPhone应用程序中使用。XML(Extensible Markup Language)是一种标记语言,常用于存储和传输结构化数据...
XMLParser是一种用于解析XML文档的C++实现,它旨在提供简单、小巧且稳定的解决方案,以处理XML数据。XML,即可扩展标记语言,是用于存储和传输数据的标准格式,广泛应用于配置文件、数据交换和文档结构化等领域。XML...
同时,也可以参考网络上的一些对比文章,例如《How To Choose The Best XML Parser For Your iPhone Project》,这是一篇较全面的比较分析文章,能够为开发者提供决策参考。 在使用过程中,开发者需要关注的不仅仅...
uNokSoft XML Parser 2.0 for PowerBuilder
Oracle数据库系统提供了强大的XML处理能力,这主要体现在其内置的几个PL/SQL包上,如DBMS_XMLDOM、DBMS_XMLPARSER和DBMS_XMLQUERY。这些包为开发者提供了处理XML文档的一整套工具,使得在数据库环境中进行XML数据的...
unity3d 读取 xml 插件 XMLParser。 unity3d 读取 xml 插件 XMLParser Unity 5.3。 unity3d读取xml插件XMLParser,用于读取xml文件 unity3d xml XMLParser
XML解析器主要有两种类型:DOM(Document Object Model)解析器和SAX(Simple API for XML)解析器。 1. DOM解析器:它将整个XML文档加载到内存中,形成一个树形结构。这样做的好处是允许开发者通过遍历树来访问和...
The information can be retrieved from the XML file, with the help of JAVA external library JDOM, which is a convenient kind of Java external library specially used to deal with the XML files....
提供对Xml文件的解析功能,xml, parser
标题提到的“xmlParser library for S60”是一个专为S60平台定制的XML解析库,由C++编写,解决了原生库在处理静态变量上的限制。 描述中提到,原始的xmlParser库由于S60平台对于静态变量的限制,不能直接在S60上...
Contains TXmlParser, the XML parser TXmlScanner and TEasyXmlScanner, two easy to use event style VCL/CLX wrappers for the TXmlParser Package files for Delphi and Kylix.
一个老外用C++写的xml打包和解析的类,很简单,但是功能很强大,并且是跨平台的,我这两年一直在用,强烈建议大家使用!!这是我目前用过的最好用的xmlparser.就包括两个文件xmlparser.h和xmlparser.cpp
XMLParser是专门为Unity3D设计的一个插件,用于方便、高效地读取XML文件。 XML是一种结构化数据格式,它以层次结构表示数据,易于人类阅读和机器解析。在Unity3D项目中,XMLParser插件提供了以下关键功能: 1. **...
XmlParser 是另一种用于解析XML的工具,它是基于SAX(Simple API for XML)的解析器,适合处理大体积的XML文件,因为它采用事件驱动的方式,不会一次性加载整个XML文档到内存中。这种方式可以有效避免内存消耗过大的...
XMLParser遵循SAX(Simple API for XML)解析模式,它不是一次性加载整个XML文档到内存,而是逐步解析XML流。这使得XMLParser特别适合处理大体积的XML文件,因为它减少了内存消耗。XMLParser会触发一系列的代理方法...
一个老外用C++写的xml打包和解析的类,很简单,但是功能很强大,并且是跨平台的,我这两年一直在用,强烈建议大家使用!!...就包括两个文件xmlparser.h和xmlparser.cpp 这是2008.03.09的最新版本。
XMLParser是iOS应用中常用的解析XML数据的框架。在iOS开发中,XML作为一种常见的数据交换格式,被广泛用于网络通信,因为它结构清晰、易于解析。本篇将详细讲解XMLParser在iOS应用中的使用,以及如何通过提供的源码...
"XML-Parser-2.4.4"是基于C语言实现的一个XML解析库,具有高度的可移植性,能够在多种操作系统和平台上运行。 XML-Parser-2.4.4源码中包含的关键知识点如下: 1. **基础数据结构**:XML解析器的核心在于构建适当的...
ExpatXML是一个轻量级的C语言实现的XML解析库,它被广泛用于处理XML文档。这个库的主要优点是它的高效性和跨平台性,使得它成为嵌入式系统和资源有限环境的理想选择。在本文中,我们将深入探讨ExpatXML的工作原理、...
BREW平台本身没有提供XML解析的API, 此解析器基于开源解析器MCBXML,然后移植到BREW平台,属我的原创。 压缩包里包括移植完成的MCBXML头文件和c文件,另外提供了一段代码(只包括.c文件,其它的需要自己创建)和一个...