如何解析iphone sdk中的嵌套XML

时间:2011-05-30 09:48:08

标签: iphone

我想解析下面的XML,但我很困惑如何解析它,因为我在一个记录中有多个嵌套。任何人都可以给我一个建议吗?

<daily_record>
    <date>2011-04-17</date>
    <gold>
        <team_image>http://hockeypicks.com/ConfiMeter/nhl_images/vancouver.bmp</team_image>
        <percentage>56%</percentage>
        <status>WIN</status>
    </gold>

    <silver>
        <team_image>http://hockeypicks.com/ConfiMeter/nhl_images/detroit.bmp</team_image>
        <percentage>66%</percentage>
        <status>LOST</status>
    </silver>

    <bronze>
        <team_image>http://hockeypicks.com/ConfiMeter/nhl_images/nashville.bmp</team_image>
        <percentage>66%</percentage>
        <status>LOST</status>
    </bronze>
    <avg_conf>56.22%</avg_conf>
</daily_record>

1 个答案:

答案 0 :(得分:5)

NSData *data=[NSData dataWithContentsOfURL:myUrl.mURL];
NSString *string=[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
DDXMLDocument *doc=[[[DDXMLDocument alloc] initWithXMLString:string options:0 error:nil] autorelease];
NSArray *docs=[doc nodesForXPath:@"//item" error:nil];
NSLog(@"Doc Count%i",[docs count]);

for (DDXMLElement *documents in docs)
{}

然后在这个for循环中你可以这样做:

DDXMLNode *childNode=[documents childAtIndex:k];
DDXMLElement *childElement=(DDXMLElement *)childNode;
book.title=[childElement stringValue] ;
book.podLink=[[childElement attributeForName:@"url"] stringValue];//For Attributes