Main menu

Post navigation

Fast XML parsing in iOS / Objective-C with TBXMLEx

Have you ever wondered which is the best way to parse a XML file in Objective-C / iOS? There are certainly a lot of options available to those who know where to lot at, but many of them are just awful to use: hard to configure, cryptic API, inexistent documentation and so forth.

Ray Wenderlich once wrote a post called “How To Choose The Best XML Parser for Your iPhone Project“, which greatly covers many XML parsers available to iOS, and was there that I discovered TBXML, which is by far one of the best parsers available to iOS. While TBXML is great, it’s “non-OO” API and some intolerance to bad formed XML had me wonder if there were anything I could do to make it better.

With that motivation, I created an extension to TBXML called “TBXMLEx” (TBXML with Extensions) which adds some syntax sugar on top of the original library and better handle bad XML files, all of that with a more OO-friendly interface.

The design goals for TBXMLEx are (taken for the original TBXML goals):

2 thoughts on “Fast XML parsing in iOS / Objective-C with TBXMLEx”

Congratulations Steil. This is a very interesting XML parsing approach, but I really would like to know how do you think this solution can be used in a more dynamic way, I mean, without to hard coding the sought attributes?

do you have a more concrete example? In your comment you said about not hard coding the attributes names, is that right? Or what you really want to achieve is to parse the entire XML without knowing beforehand the *element* names?

As of the attributes, you can use “NSDictionary *allAttributes = fileNode.attributes” and iterate over the keys. However, if what you really want is to parse the file without knowing the element names, I’ll have to expose such API. TBXML itself has this option, but my extension is hiding it yet. I can add that feature, if needed.