`

How To Choose The Best XML Parser for Your iPhone Project

    博客分类:
  • ios
 
阅读更多

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.

  • 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 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

XML Parser Stats Display in Test 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):

Parsing Time By Parser

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):

Memory Usage By Parser

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:

  NSXML libxml2 – SAX TBXML TouchXML KissXML TinyXML GDataXML libxml2 – DOM
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

分享到:
评论

相关推荐

    XMLParser iphone

    XMLParser在iOS开发中是一种常用的解析XML数据的工具,它允许开发者将XML文件转换为可操作的数据结构,便于在iPhone应用程序中使用。XML(Extensible Markup Language)是一种标记语言,常用于存储和传输结构化数据...

    XMLParser(XML解析代码 C++版)

    XMLParser是一种用于解析XML文档的C++实现,它旨在提供简单、小巧且稳定的解决方案,以处理XML数据。XML,即可扩展标记语言,是用于存储和传输数据的标准格式,广泛应用于配置文件、数据交换和文档结构化等领域。XML...

    iOS开发中常见的解析XML的类库以及简要安装方法

    同时,也可以参考网络上的一些对比文章,例如《How To Choose The Best XML Parser For Your iPhone Project》,这是一篇较全面的比较分析文章,能够为开发者提供决策参考。 在使用过程中,开发者需要关注的不仅仅...

    uNokSoft XML Parser 2.0 for PowerBuilder

    uNokSoft XML Parser 2.0 for PowerBuilder

    DBMS_XMLDOM DBMS_XMLPARSER DBMS_XMLQUERY 文档

    Oracle数据库系统提供了强大的XML处理能力,这主要体现在其内置的几个PL/SQL包上,如DBMS_XMLDOM、DBMS_XMLPARSER和DBMS_XMLQUERY。这些包为开发者提供了处理XML文档的一整套工具,使得在数据库环境中进行XML数据的...

    unity3d 读取 xml 插件 XMLParser Unity 5.3

    unity3d 读取 xml 插件 XMLParser。 unity3d 读取 xml 插件 XMLParser Unity 5.3。 unity3d读取xml插件XMLParser,用于读取xml文件 unity3d xml XMLParser

    XMLparser.rar

    XML解析器主要有两种类型:DOM(Document Object Model)解析器和SAX(Simple API for XML)解析器。 1. DOM解析器:它将整个XML文档加载到内存中,形成一个树形结构。这样做的好处是允许开发者通过遍历树来访问和...

    xml parser

    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

    提供对Xml文件的解析功能,xml, parser

    xmlParser library for Symbian S60

    标题提到的“xmlParser library for S60”是一个专为S60平台定制的XML解析库,由C++编写,解决了原生库在处理静态变量上的限制。 描述中提到,原始的xmlParser库由于S60平台对于静态变量的限制,不能直接在S60上...

    XML Parser

    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.

    xmlParser.zip_C++ 大 XML_XML解析_xml parser_xmlparser_xmlparser v2.

    一个老外用C++写的xml打包和解析的类,很简单,但是功能很强大,并且是跨平台的,我这两年一直在用,强烈建议大家使用!!这是我目前用过的最好用的xmlparser.就包括两个文件xmlparser.h和xmlparser.cpp

    unity3d读取xml插件XMLParser

    XMLParser是专门为Unity3D设计的一个插件,用于方便、高效地读取XML文件。 XML是一种结构化数据格式,它以层次结构表示数据,易于人类阅读和机器解析。在Unity3D项目中,XMLParser插件提供了以下关键功能: 1. **...

    Unity3D中Mono.Xml和XmlParser的使用

    XmlParser 是另一种用于解析XML的工具,它是基于SAX(Simple API for XML)的解析器,适合处理大体积的XML文件,因为它采用事件驱动的方式,不会一次性加载整个XML文档到内存中。这种方式可以有效避免内存消耗过大的...

    IOS开发 XmlParser Demo

    XMLParser遵循SAX(Simple API for XML)解析模式,它不是一次性加载整个XML文档到内存,而是逐步解析XML流。这使得XMLParser特别适合处理大体积的XML文件,因为它减少了内存消耗。XMLParser会触发一系列的代理方法...

    xmlparser library

    一个老外用C++写的xml打包和解析的类,很简单,但是功能很强大,并且是跨平台的,我这两年一直在用,强烈建议大家使用!!...就包括两个文件xmlparser.h和xmlparser.cpp 这是2008.03.09的最新版本。

    xmlparser.zipIOS应用例子源码下载

    XMLParser是iOS应用中常用的解析XML数据的框架。在iOS开发中,XML作为一种常见的数据交换格式,被广泛用于网络通信,因为它结构清晰、易于解析。本篇将详细讲解XMLParser在iOS应用中的使用,以及如何通过提供的源码...

    XML-Parser-2.4.4 官方源码

    "XML-Parser-2.4.4"是基于C语言实现的一个XML解析库,具有高度的可移植性,能够在多种操作系统和平台上运行。 XML-Parser-2.4.4源码中包含的关键知识点如下: 1. **基础数据结构**:XML解析器的核心在于构建适当的...

    c语言版的XML parser

    ExpatXML是一个轻量级的C语言实现的XML解析库,它被广泛用于处理XML文档。这个库的主要优点是它的高效性和跨平台性,使得它成为嵌入式系统和资源有限环境的理想选择。在本文中,我们将深入探讨ExpatXML的工作原理、...

    XML parser for BREW

    BREW平台本身没有提供XML解析的API, 此解析器基于开源解析器MCBXML,然后移植到BREW平台,属我的原创。 压缩包里包括移植完成的MCBXML头文件和c文件,另外提供了一段代码(只包括.c文件,其它的需要自己创建)和一个...

Global site tag (gtag.js) - Google Analytics