用TouchXML解析子节点

| 我正在使用TouchXML解析我的RSS。  新的XML
 <channel>
 <item> 
 <title> </title>
  <social>
   <views>
    <total_views>2</total_views>
   </views>
   <reviews>
    <average_rating>2</average_rating>
   </reviews>
  </social>
 </item>
 </channel>
我目前正在解析XML,并将它与ѭ1一起传递到我的详细信息视图中,以获得像这样的文章标题:
 [rssData objectForKey: @\"title\" ];
但是,如何显示恒星的价值,它是率的子节点,而率是社会的子节点? 我已经试过了:
[rssData objectForKey: @\"social/rate/stars\" ];
但这是行不通的。请告诉我如何。     
已邀请:
        我根据原始答案得出的整个前提似乎都是错误的。如果您使用了以下代码段,
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);
}    
    
        试试这个,您需要传递readNode值,例如\“ channle \”,它将返回数组,包括子子节点
 -(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;
    }
    

要回复问题请先登录注册