2011-05-30 59 views
0

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

<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> 
+0

阅读此链接http://codesofa.com/blog/archive/2008/07/23/make -nsxmlparser-your-friend.html我希望它可以帮助你 – 2011-09-06 10:05:18

回答

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 
+0

- 谢谢你的回复,但你可以更详细地解释bcoz我没有得到你想说的东西!!! – Priyanka 2011-05-31 08:28:46

+0

我已编辑它...希望这有助于...不断打扰我,直到你得到它..! – Suny 2011-05-31 09:40:20