使用 TouchXML 解析子节点 [英] Parsing subnode with TouchXML

查看:25
本文介绍了使用 TouchXML 解析子节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm using TouchXML to parse my RSS. The new 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 一起传递到我的文章标题的详细视图,如下所示:

I currently parsing the XML and passing it with initWithData to my detail view for the title of the article like this:

 [rssData objectForKey: @"title" ];

但是我如何显示星星的价值,它是 rate 的子节点,而 rate 是来自 social 的子节点?

But how do I show the value of stars its a subnode of rate and rate is a subnode from social?

我已经试过了:[rssData objectForKey: @"social/rate/stars" ];

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

But it won't work. Please tell me how.

推荐答案

我基于原始答案的整个前提似乎是错误的.如果您使用了以下代码段,

The entire premise that I based my original answers seem to be wrong. If you've used the following snippet,

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 元素的子元素被存储为字符串值,任何提取它们的合理方法都将丢失.我建议您存储节点而不是去创建字典.这是提取评分的示例,

to generate your code than children of social element are stored as a string value and any reasonable means to extract them are lost. I would suggest that you store the nodes rather than go about creating the dictionary. Here's the example of extracting the rating,

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

这篇关于使用 TouchXML 解析子节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆