首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用TouchXML解析子节点

用TouchXML解析子节点
EN

Stack Overflow用户
提问于 2011-06-21 22:11:09
回答 2查看 1.1K关注 0票数 2

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

代码语言:javascript
复制
 <channel>
 <item> 
 <title> </title>
  <social>
   <views>
    <total_views>2</total_views>
   </views>
   <reviews>
    <average_rating>2</average_rating>
   </reviews>
  </social>
 </item>
 </channel>

目前,我解析该XML并将其与initWithData一起传递给本文标题的详细视图,如下所示:

代码语言:javascript
复制
 [rssData objectForKey: @"title" ];

但是,我如何显示星星的价值,它是速率的子节点,而速率是来自社会的子节点?

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

但这行不通。请告诉我怎么做。

EN

回答 2

Stack Overflow用户

发布于 2011-06-22 00:13:06

我最初的答案的前提似乎是错误的。如果你使用了下面的片段,

代码语言:javascript
复制
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元素的子元素存储为字符串值,并且丢失了提取它们的任何合理方法。我建议您存储节点,而不是创建字典。下面是提取评级的例子,

代码语言:javascript
复制
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
EN

Stack Overflow用户

发布于 2013-06-13 12:44:12

尝试这样做,您需要像“信道”一样传递readNode值,它将返回数组,包括子子节点。

代码语言:javascript
复制
 -(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;
    }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6432638

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档