从RSS描述获取URL

时间:2014-01-29 13:33:02

标签: ios objective-c rss

我需要帮助从RSS源获取URL。我试图从描述中获取图像URL,但是当我尝试使用它时它是空的。

以下是Feed:

<item>
<title>Photo</title>
<description>
<img src="http://24.media.tumblr.com/c0d9f18c016c3bd45a84edc58d5c9d1b/tumblr_mv6nhvj0Vs1qjbzjlo1_500.jpg"/><br/><br/>
</description>
<link>
http://littleheartrecords.tumblr.com/post/64965673651
</link>
<guid>
http://littleheartrecords.tumblr.com/post/64965673651
</guid>
<pubDate>Thu, 24 Oct 2013 13:05:55 -0400</pubDate>
</item>

这是我得到它的地方:

else if ([elementName isEqual:@"description"])
    {
        currentString = [[NSMutableString alloc]init];
        [self setInfoString:[attributeDict valueForKey:@"img src"]];
    }

这是我尝试使用它的地方:

NSLog(@"infoString: %@", [item infoString]);
    NSURL *imageLink = [NSURL URLWithString:[item infoString]];
    NSData *data = [NSData dataWithContentsOfURL:imageLink];
    UIImage *image = [[UIImage alloc]initWithData:data];

2 个答案:

答案 0 :(得分:0)

试试这个

didStartElement:

中的

else if ([elementName isEqual:@"description"]) {
    ...
    isDescription = YES;
} else if ([elementName isEqual:@"img"]) {
    if (isDescription) {
         NSString *urlString = [attributeDict valueForKey:@"src"];
         NSLog(@"urlString : %@", urlString);
        [self setInfoString:urlString];
        //or
        [item setInfoString:urlString];
    }
}

并在didEndElement:

else if ([elementName isEqual:@"description"]) {
    ...
    isDescription = NO;
}

答案 1 :(得分:0)

使用XMLReader类非常容易使用。

Example

NSData *data = ...; // some data that can be received from rss
NSError *error = nil;
NSDictionary *dict = [XMLReader dictionaryForXMLData:data 
                                         options:XMLReaderOptionsProcessNamespaces 
                                           error:&error];

注意使用NSDictionary返回的classes获取url string