How to choose the best XML Parser for your iPhone Project

Source: Internet
Author: User
Tags xml parser

Http://www.raywenderlich.com/553/how-to-chose-the-best-xml-parser-for-your-iphone-project

In summary, using libxml2 is quite good. Regardless of XML size or XPath support

Here 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 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.

  • ASAX ParserIs 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.
  • AA Dom parserReads 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 participant eces.

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 encoded 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 already ded 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 cocould be reading a large XML document from the network and displaying data that you're re reading for it to the user while you're 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 known Ming 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 ents 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 ~ 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 wocould be an ideal way to test out how these varous APIs med 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 has ded XPath support, I used it for a single lookup, because I felt it represented the way the library wocould 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 got med 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 varous 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 extends 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 saved med faster than libxml2' s SAX Parser, which I had thought wocould 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 varous 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 wocould 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 ve discussed above:

  Nsxml Libxml2-Sax Tbxml Touchxml Kissxml Tinyxml Gdataxml Libxml2-dom
Supported 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 wantRead small XML documents ents, 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 bothRead and Write small XML documents ents, Again performance doesn' t matter as much as functionality and pointer 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 wantRead 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?

  • NsxmlIs a decent choice if you're dealing with relatively small statements, and you don't feel like adding a third party library to the SDK.
  • TinyxmlCocould 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!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.