使用 TouchXML 解析子节点

发布于 2024-11-16 10:37:51 字数 688 浏览 3 评论 0原文

我正在使用 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" ];

But how do I show the value of stars it速率的子节点和速率是来自社交的子节点吗?

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

但它不起作用。请告诉我怎么做。

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>

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" ];

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

I have tried this: [rssData objectForKey: @"social/rate/stars" ];

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

匿名的好友 2024-11-23 10:37:51

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

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

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

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);
}    
失去的东西太少 2024-11-23 10:37:51

尝试这个,你需要传递 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;
    }

try this , you need to pass readNode value like "channle", and it will return array, including child subnode

 -(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;
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文