what is the best way to load a xml file as an parsed object in objective c?
Look into the NSXmlParser. It can read xml and gives you methods you can override to process the contents.
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.
I recommend using TouchXML. Great documentation and relatively easy to use compared with the NSXMLParser.
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];
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