iPhone Development - XMLParser vs. libxml2 vs. TouchXML

Mustafa picture Mustafa · Jan 2, 2009 · Viewed 18.1k times · Source

I cannot find comparison of these parsing technique. Which one is most commonly used?

Regards. Mustafa

Answer

Robbie Hanson picture Robbie Hanson · Jan 2, 2009

NSXMLParser is a SAX parser, meaning it steps through the XML document, and informs you (via delegate methods) of various events (such as starting an xml node, finding attributes, etc). This type of XML processing is best for parsing huge documents, and when you only need to retrieve a tiny amount of data from a big file.

In contrast to SAX is the DOM model, where the entire XML tree is parsed into memory (usually with a single call), and then you can freely explore the XML document from the presented API. This is generally much easier to work with, as you get random access to the entire xml document.

So the first question one should answer is the SAX vs DOM question. If you're woring with big XML files (say 10 MB or bigger), you may want to stick with SAX. If you're working with small XML files, or tiny XML fragments, it's often much easier to use DOM.

If indeed you decide to go with DOM, you've got a few options.

libxml2 is a very powerful API written in C. It's mainly for DOM style use, but has other options if you become experienced with it. But, as it's written in C, it's not often attractive to those more familiar with Objective-C (objects, and autorelease), or those coming over from the .Net world. Thus, there is a need/desire for a native Objective-C wrapper around libxml to make it more familiar. TouchXML is one such wrapper. It works well if you only need read access to the XML. If you want to alter the XML, or create XML fragments/documents from scratch, I'd recommend going with KissXML.