views:

137

answers:

5

what is the best way to load a xml file as an parsed object in objective c?

A: 

Look into the NSXmlParser. It can read xml and gives you methods you can override to process the contents.

Derek Clarkson
+1  A: 

If you know some C, the best (i.e. fastest parsing, lowest memory footprint) way to parse XML into Objective-C objects is probably via the SAX event parser in libxml2. Apple has a sample project called XMLPerformance which demonstrates how this is done.

Alex Reynolds
+1  A: 

I recommend using TouchXML. Great documentation and relatively easy to use compared with the NSXMLParser.

Kevin Sylvestre
+2  A: 

Check out TouchXML. It's the easiest library I've found so far. Supports xpath on a basic level. http://code.google.com/p/touchcode/wiki/TouchXML. Here's a sample (code removed) from a recent project:

CXMLDocument *parser = [[CXMLDocument alloc] initWithData:theData encoding:NSUTF8StringEncoding options:0 error:nil];
NSArray *nodes = [parser nodesForXPath:@"//item" error:nil];

for (CXMLElement *resultElement in nodes)
{
    NSMutableDictionary *data = [[NSMutableDictionary alloc] initWithCapacity:0];

    // Create a counter variable as type "int"
    int counter;

    // Loop through the children of the current node and add to dictionary
    for (counter = 0; counter < [resultElement childCount]; counter++)
    {
        // Add each field to the dictionary with the node name as
        // key and node value as the value
        [data setObject:[[resultElement childAtIndex:counter] stringValue]
                 forKey:[[resultElement childAtIndex:counter] name]];
    }

    // do stuff with the dictionary by named keys
    // ...

    // release dict
    [data release];
}

[parser release];
Typeoneerror
+1  A: 

I found this article very helpful and I'm using TBXML since it shows the best performance for memory usage and for speed.

XML performance test - updated

BTW, the code of the project is attached and you can learn from examples. NSXMLParser shows the worst performance

Nava Carmon
That's a great link.
Alex Reynolds