40

There are a million different XML parsers for the iPhone. I have a medium sized XML file that contains alot of duplicate tags (at different points in the hierarchy). I was thinking about TBXML but I was concerned about its lack of XPath support. For instance lets say my XML file looked like this.

<blog>
   <author> foo1 </author>
   <comments>

       <comment>
            <text>
                <![CDATA[ HTML code that must be extracted ]]>
            </text>
            <author>foo2</author>
       </comment>

       <comment>
          <text>
              <![CDATA[ Here is another post ]]> 
          </text>
          <author>foo1</author>
      </comment>

   </comments>
</blog>

Basically my requirements are that I need to be able to extract that cdata. And know whether it is the blog author a comment author.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
endy
  • 3,872
  • 5
  • 29
  • 43

5 Answers5

51

Best comparison I've seen on the subject:

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

The difference between the slowest and fastest parsers in his case was a factor of 2. Depending on your feature requirements, there are definitely parsers that can cut your parsing time in half.

Daniel Amitay
  • 6,677
  • 7
  • 36
  • 43
  • 1
    Some of the stuff in the article is getting outdated, but overall still very helpful (just posting so people can see this answer is still valid as of the end of July) – Cameron Jul 28 '11 at 15:21
  • 1
    Note. KissXML moved to Github and still actively being updated. https://github.com/robbiehanson/KissXML – eonil Dec 19 '11 at 08:09
  • 1
    In case anybody wants to re-run the tests with updated parsers: https://github.com/jansanz/iOSXMLPerformance (I have also included 2 more that weren't originally in the test). – Jan S. Nov 23 '12 at 04:34
  • TouchXML is also good. – Gajendra K Chauhan Jul 01 '14 at 03:47
31

Check out RaptureXML. It's not official XPath behavior, but it's pretty close. Your query would be:

[rxml iterate:@"comments.comment" with: ^(RXMLElement *e) {
    if ([[e child:@"author"].text isEqualToString:@"foo1"]) {
        NSLog(@"Blog Text is %@.", e.text);
    } else {
        NSLog(@"Comment Text is %@.", e.text);
    }
}];

UPDATE: RaptureXML now supports XPath!

ZaBlanc
  • 4,679
  • 1
  • 35
  • 38
4

You should look at these libraries, in order :)

  1. Ono, powered by libxml2
  2. XMLDictionary
  3. RaptureXML

Ono has the the cool AFOnoResponseSerializer which can be integrated with AFNetworking

onmyway133
  • 45,645
  • 31
  • 257
  • 263
1

I made this new simple and lightweight XML parser for iOS in Swift - AEXML

You can use it to read XML data like this:

let someValue = xmlDocument["element"]["child"]["anotherChild"].value

or you can use it to build XML string like this:

let document = AEXMLDocument()
let element = document.addChild("element")
element.addChild("child")
document.xmlString // returns XML string of the entire document structure

I hope it will be useful for someone.

tadija
  • 2,981
  • 26
  • 37
0

As you have requirement for medium sized XML

TinyXML could be an good 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.

For More information you can go through this link

Mark
  • 6,762
  • 1
  • 33
  • 50
Shan
  • 131
  • 7