How to extract an image src from RSS feed

不问归期 提交于 2020-01-07 08:21:20

问题


I'm making a news app in ios. For that i need to fetch image from rss using objective c

These appear in the node of the RSS feed (see example below)

<description> <![CDATA[ <div><img width="745" height="410" src="http://thisisthelink.com/to_my_image.jpg" class="attachment-large wp-post-image" alt="alt tag" style="margin-bottom: 15px;" /></div>Good morning. Normally Friday is a busy day as we prepare for the weekend&#8217;s game. Arsene Wenger holds his press conference, we get the latest team news and so on,... ]]> </description>

I currently get the <title> and <content> with no issue, but I need to extract just the image source so I can put that into my imageView and in turn into my TableRow.

This is what I'd like to trim the string down to from the above

http://thisisthelink.com/to_my_image.jpg

Don't know how to proceed help me out of this.. Also i don't know how to include calender in my app so that user can fetch data in any particular date.


回答1:


I found using hpple quite useful to parse messy HTML. Hpple project is a Objective-C wrapper on the XPathQuery library for parsing HTML. Using it you can send an XPath query and receive the result .

Requirements:

-Add libxml2 includes to your project

Menu Project->Edit Project Settings Search for setting "Header Search Paths" Add a new search path "${SDKROOT}/usr/include/libxml2" Enable recursive option -Add libxml2 library to to your project

Menu Project->Edit Project Settings Search for setting "Other Linker Flags" Add a new search flag "-lxml2" -From hpple get the following source code files an add them to your project:

TFpple.h
TFpple.m
TFppleElement.h
TFppleElement.m
XPathQuery.h
XPathQuery.m

Code Example

 #import "TFHpple.h"

   NSData *data = [[NSData alloc] initWithContentsOfFile:@"example.html"];

   TFHpple *parser = [TFHpple hppleWithHTMLData:data];

    NSString *xpathQueryString = @"//img";
    NSArray *nodes = [parser searchWithXPathQuery:xpathQueryString];

    for (TFHppleElement *element in nodes)
    {
        NSString *src = [element objectForKey:@"src"];
        NSLog(@"img src: %@", src);
    }


来源:https://stackoverflow.com/questions/23823042/how-to-extract-an-image-src-from-rss-feed

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!