Processing XML on the iPhone

Posted on 5 May 2009 by mike

For Tutorial 6 I needed to process an XML document produced by a tilemap editor. I was most disappointed to find that the iPhone SDK does not come with very much in the way of classes to process XML.

The one class it does have NSXMLParser is not bad but its VERY VERY slow and uses loads of memory.

So, based on this I started to look around at what else I could use. There is plenty of chatter on forums about the lack of a good XML API on the iPhone SDK and I soon found TouchXML.

This is an Objective-C wrapper for the libxml2 library which it appears Apple themselves use. What I liked about it was that it followed the NSXML structure of the XML APIs Apple provides within Cocoa on the Mac, so the Apple docs pretty much can be used to work out how TouchXML works.

I followed the minimal instructions and managed to get it working in my project and it all seemed great, very quick and just what I needed. Then I came across a problem with xPath. For some reason I could not get my xPath expressions to work with TouchXML. I am not sure if this was a namespace issues, as my file was not using namespaces, but I could not get past this issue so I decided to keep looking.

The next API I came across was KissXML. This was created based on TouchXML although it goes further. TouchXML allows you to read XML whilst KissXML allows you to both read and write XML. KissXML was again very easy to get working in my project and again follows the NSXML API closely.

It again was quick, although it did not seem as quick as TouchXML. This time it was also able to handle my simple xPath without any problems and so this is the API I have stuck with.

I've not had a chance to check out many of the features yet, but it seems like a great solution to the lack of a good XML API from Apple in the SDK.

Mike

blog comments powered by Disqus