2011-06-21 51 views
2

我正在使用TouchXML解析我的RSS。 新的XML使用TouchXML解析子節點

<channel> 
<item> 
<title> </title> 
    <social> 
    <views> 
    <total_views>2</total_views> 
    </views> 
    <reviews> 
    <average_rating>2</average_rating> 
    </reviews> 
    </social> 
</item> 
</channel> 

我目前解析XML並以initWithData把它傳遞給我的詳細信息視圖的文章像這樣的標題:

[rssData objectForKey: @"title" ]; 

但是我怎麼顯示值的星星它的速率和利率的子節點是從社會的子節點?

我已經試過這樣:[rssData objectForKey: @"social/rate/stars" ];

但它不會工作。請告訴我如何。

回答

1

我基於原始答案的整個前提似乎是錯誤的。如果你使用下面的代碼片段,

for (CXMLElement *node in nodes) { 
    NSMutableDictionary *item = [[NSMutableDictionary alloc] init]; 
    int counter; 
    for(counter = 0; counter < [node childCount]; counter++) { 
     // common procedure: dictionary with keys/values from XML node 
     [item setObject:[[node childAtIndex:counter] stringValue] forKey:[[node childAtIndex:counter] name]]; 
    } 

    [results addObject:item]; 
    [item release]; 
}  

可以生成代碼比social元素的兒童被存儲爲一個字符串值和任何合理的方式來提取它們都將丟失。我建議你存儲節點,而不是去創建字典。這裏的提取等級的例子,

NSString * xmlString = @"<channel> <item> <title> </title> <social> <views> <total_views>2</total_views> </views> <reviews> <average_rating>2</average_rating> </reviews> </social> </item> </channel>"; 

CXMLDocument * xmlDocument = [[[CXMLDocument alloc] initWithXMLString:xmlString options:0 error:nil] autorelease]; 
NSArray * nodes = [xmlDocument nodesForXPath:@"//item" error:nil]; 

for (CXMLElement * node in nodes) { 

    NSString * title = [[node nodeForXPath:@"title" error:nil] stringValue]; 
    NSString * average_rating = [[node nodeForXPath:@"social/reviews/average_rating" error:nil] stringValue] ; 
    NSLog(@"%@", title); 
    NSLog(@"%@", average_rating); 
}  
+0

:79:警告:' NSDictionary'可能不會響應'-nodesForXPath:錯誤:' – ruben

+0

假設它是一個節點。由於它是一個字典,因此可以使用'valueForKeyPath:'來獲取值。更新我的答案以反映這一點。 –

+0

由於未捕獲的異常'NSUnknownKeyException'而終止應用,原因:'[ valueForUndefinedKey:]:該類不是關鍵值,它們與關鍵評論的編碼兼容。 – ruben

0

試試這個,你需要通過像「channle」 readNode值,它將返回數組,包括兒童子節點

-(NSMutableArray *)parseNodeFromXML:(NSString *)strXML readForNode:(NSString *)strReadValue 
    { 
     NSError *theError = NULL; 
     CXMLDocument *theXMLDocument = [[CXMLDocument alloc] initWithXMLString:strXML options:0 error:&theError] ; 

     NSMutableArray *arrReturn = [[NSMutableArray alloc] init]; 
     NSArray *nodes = [[theXMLDocument rootElement] nodesForXPath:strReadValue error:nil]; 

     for (CXMLNode *itemNode in nodes) 
     { 

      NSMutableDictionary *dic = [[NSMutableDictionary alloc] init]; 

      // PROCESS FOR READ ELEMENT ATTRIBUTES IN CURRENT NODE ------------------------- 

      if([itemNode isMemberOfClass:[CXMLElement class]]){ 
       CXMLElement *elements=(CXMLElement*)itemNode; 
       NSArray *arAttr=[elements attributes]; 

       for (int i=0; i<[arAttr count]; i++) { 
        if([[arAttr objectAtIndex:i] name] && [[arAttr objectAtIndex:i] stringValue]){ 
         [dic setValue:[[arAttr objectAtIndex:i] stringValue] forKey:[[arAttr objectAtIndex:i] name]]; 
        } 
       } 
      } 


      // PROCESS FOR READ CHILD NODE IN CURRENT NODE ------------------------- 

      for (CXMLNode *childNode in [itemNode children]) 
      { 
       // NSLog(@"count -- %d --- %@",[childNode childCount],[childNode children]); 

       // IF ANY CHILD NODE HAVE MULTIPLE CHILDRENS THEN DO THIS....- 
       if ([childNode childCount] > 1) 
       { 
        NSMutableDictionary *dicChild = [[NSMutableDictionary alloc] init]; 
        for (CXMLNode *ChildItemNode in [childNode children]) 
        { 
         [dicChild setValue:[ChildItemNode stringValue] forKey:[ChildItemNode name]]; 
        } 
        [dic setValue:dicChild forKey:[childNode name]]; 
       }else 
       { 
        [dic setValue:[childNode stringValue] forKey:[childNode name]]; 
       } 

      } 

      [arrReturn addObject:dic]; 
     } 

     // NSLog(@"%@",arrReturn); 
     return arrReturn; 
    }