在RSS feed中查找图像 - iOS

时间:2013-11-29 23:16:53

标签: ios image rss

我正在为我的网站制作一个应用程序,我需要在表格视图中包含图像。

RSS图片我试图找到:

<description>
<![CDATA[
<img width="1100" height="1010" src="http://www.x86cam.com/wp-content/uploads/2013/11/twilight_sparkle_with_the_twicane_by_diamondsword11-d6vn28u.png" class="attachment- wp-post-image" alt="twilight_sparkle_with_the_twicane_by_diamondsword11-d6vn28u" style="display: none" />I will not give any links, but I just need to warn y&#8217;all. Watch out for spoilers all over sites. Source: Here
]]>
</description>

我不知道如何在此找到图片标记。

#pragma mark - parsing of RssFeed Values

-(void) parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{
    element = elementName;

    if ([element isEqualToString:@"item"]) {
        item = [[NSMutableDictionary alloc]init];
        title = [[NSMutableString alloc]init];
        link = [[NSMutableString alloc] init];
        desc = [[NSMutableString alloc] init];
        image = [[NSMutableString alloc] init];
        pubDate = [[NSMutableString alloc] init];
    }
}

-(void) parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{
    if ([elementName isEqualToString:@"item"]) {

        [item setObject:title forKey:@"title"];
        [item setObject:link forKey:@"link"];
        [item setObject:desc forKey:@"description"];
        [item setObject:pubDate forKey:@"pubDate"];
        [feeds addObject:[item copy]];

    }
}

-(void) parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
    if ([element isEqualToString:@"title"]) {
        [title appendString:string];
    }else if ([element isEqualToString:@"link"]){
        [link appendString:string];
    }else if ([element isEqualToString:@"description"]){
        [desc appendString:string];
    }else if ([element isEqualToString:@"pubDate"]){
        [pubDate appendString:string];
    }
}

- (void)parserDidEndDocument:(NSXMLParser *)parser {

    [self.tableView reloadData];

}

我希望该应用解析XML并在<img>中找到<description>标记,然后获取src=""

有谁愿意并能够帮助我吗?

1 个答案:

答案 0 :(得分:0)

您要提取的图像链接位于CDATA块内,但rss解析器会忽略CDATA块。

如果需要从CDATA中提取字符串,可以在foundCDATA中使用此块:

- (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock
{

NSMutableString *cdstring = [[NSMutableString alloc] initWithData:CDATABlock encoding:NSUTF8StringEncoding];
}

现在mutablestring“cdstring”将包含:

“img width =”1100“height =”1010“src =”http://www.x86cam.com/wp-content/uploads/2013/11/twilight_sparkle_with_the_twicane_by_diamondsword11-d6vn28u.png“class =”attachment-wp -post-image“alt =”twilight_sparkle_with_the_twicane_by_diamondsword11-d6vn28u“style =”display:none“/&gt;我不会给任何链接,但我只需要警告你们。请注意所有网站上的剧透。来源:这里“

现在只需搜索src =“使用stringcontainsstring并提取链接

-cheers :) P.S:如果你可以要求rss feed的提供者在没有CDATA的正常块中提供它,你可以使用以下方法轻松提取链接:

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName   namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:  (NSDictionary *)attributeDict

// In this method parameter 'attributeDict' will return you all the sub attributes of main attribute.
// In your case its 'url' of Picture.

// I hope this will help you. Check this out.
if ([element isEqualToString:@"img"]){
[attributeDict valueForKey:<@"src">]
}

希望有所帮助