使用TouchXML解析子节点

时间:2011-06-21 22:11:09

标签: objective-c xml 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并将其与initWithData一起传递到我的详细视图中,以获取文章标题,如下所示:

 [rssData objectForKey: @"title" ];

但是,我如何展示星星的价值,它的速率和速率的子节点是社交的子节点?

我试过这个:[rssData objectForKey: @"social/rate/stars" ];

但它不会奏效。请告诉我怎么做。

2 个答案:

答案 0 :(得分: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);
}    

答案 1 :(得分:0)

试试这个,你需要传递readNode值,如&#34; channle&#34;,它将返回数组,包括子子节点

 -(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;
    }