使用NSXMLParser解析RSS时访问特定的子元素 [英] Accessing specific child elements when parsing RSS with NSXMLParser

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

问题描述

亲爱的学者
我正在尝试使用以下项目元素结构解析RSS提要

Dear Scholars I am trying to parse a RSS feed with the following item element structure

<item> 
         <title>Title</title> 
         <link>http://somelink.com</link> 
         <description>The description</description> 
         <author>rss@youtube.com</author> 
         <guid isPermaLink="false">http://youtube.com/?v=XBE4AX0Iuvw</guid> 
         <pubDate>Thu, 11 Nov 2010 10:56:44 +0000</pubDate> 
         <media:title>Media Title</media:title> 
         <media:thumbnail width="120" url="http://i1.ytimg.com/vi/XBE4AX0Iuvw/default.jpg" height="90"/> 
         <media:category label="Tags"/> 
         <media:player url="http://youtube.com/?v=XBE4AX0Iuvw"/> 
         <media:credit>Credit name</media:credit> 
         <enclosure url="http://youtube.com/v/XBE4AX0Iuvw.swf" duration="104" type="application/x-shockwave-flash"/> 
</item> 

请注意标签 media:thumbnail.url ,其中我的问题是与...相关。

Please note the tag media:thumbnail.url of which my question is related to.

解析码:

// string contants found in the RSS feed
static NSString *kIDStr     = @"link";
static NSString *kNameStr   = @"thumbnail";
static NSString *kImageStr  = @"media:thumbnail";
static NSString *kArtistStr = @"description";
static NSString *kEntryStr  = @"item";


- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName
                                            namespaceURI:(NSString *)namespaceURI
                                           qualifiedName:(NSString *)qName
                                              attributes:(NSDictionary *)attributeDict
    {
        // entry: { id (link), im:name (app name), im:image (variable height) }
        //
        NSLog(@"%@",elementName); 
        // I can see all top level elements,even media:thumbnail

        if ([elementName isEqualToString:kEntryStr])
        {
            self.workingEntry = [[[AppRecord alloc] init] autorelease];
        }
        storingCharacterData = [elementsToParse containsObject:elementName];
    }

    - (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName
                                          namespaceURI:(NSString *)namespaceURI
                                         qualifiedName:(NSString *)qName
    {
        if (self.workingEntry)
        {
            if (storingCharacterData)
            {


        NSString *trimmedString = [workingPropertyString stringByTrimmingCharactersInSet:
                                           [NSCharacterSet whitespaceAndNewlineCharacterSet]];

                [workingPropertyString setString:@""];  // clear the string for next time

                if ([elementName isEqualToString:kIDStr])
                {
                    self.workingEntry.appURLString = trimmedString;
                }
                else if ([elementName isEqualToString:kNameStr])
                {        
                    self.workingEntry.appName = trimmedString;
                }
                else if ([elementName isEqualToString:kImageStr])
                {

NSLog(@"The Content: %@",workingPropertyString); // Comes back empty, I guess due to the fact it has children inside.               


// QUESTION: How do I get the value stored in media:thumbnail   .url  
//NSLog(@">>>>>>>>>>>>> %@ / %@",[elementsToParse objectAtIndex:2],workingPropertyString);



                self.workingEntry.imageURLString = trimmedString;
                }
                else if ([elementName isEqualToString:kArtistStr])
                {
                    self.workingEntry.artist = trimmedString;

                }
            }
            else if ([elementName isEqualToString:kEntryStr])
            {
                [self.workingArray addObject:self.workingEntry];  
                self.workingEntry = nil;
            }
        }

    }

似乎是一个我不熟悉的语法问题,它抓住了媒体中存储的值:thumbnail.url。有人可以帮助我这样做吗?

It seems to be a matter of syntax which I am not familiar with, which grabs the value stored in media:thumbnail.url. Can someone be as kind as to assist me in how do do so?

推荐答案

作为 url media:thumbnail 元素的属性,您需要通过属性字典访问它在 didStartElement: foundCharacters:消息(我假设您将文本累积到 workingPropertyString 中)仅发送给字符数据在元素的开始和结束标记之间。

As url is an attribute of the media:thumbnail element, you need to access it via the attributes dictionary in didStartElement:. The foundCharacters: message (which I assume is where you're accumulating text into workingPropertyString) is only sent for character data between an element's start and end tags.

这篇关于使用NSXMLParser解析RSS时访问特定的子元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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