开发者

iPhone loading xml file

开发者 https://www.devze.com 2022-12-29 14:24 出处:网络
what is the b开发者_JS百科est way to load a xml file as an parsed object in objective c?Check out TouchXML. It\'s the easiest library I\'ve found so far. Supports xpath on a basic level. http://code.g

what is the b开发者_JS百科est way to load a xml file as an parsed object in objective c?


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];


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消