2014-05-23 84 views
0

我在ios中制作新闻应用程序。为此,我需要从RSS 获取图像使用目标C如何从RSS提要中提取图像src

这些出现在RSS提要的节点(见下例)

<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>

我目前得到<title> and <content>没有问题,但我需要提取图像源,以便将其放入我的imageView中,然后放入我的TableRow中。

这是想什么,我从上面的

http://thisisthelink.com/to_my_image.jpg

不知道如何着手帮我出这个.. 此外,我不知道到修剪字符串如何在我的应用程序中包含日历,以便用户可以在任何特定日期获取数据。

回答

3

我发现使用hpple相当有用的解析凌乱的HTML。 Hpple项目是XPathQuery库中用于解析HTML的Objective-C包装器。使用它您可以发送XPath查询并接收结果。

要求:

- 添加的libxml2包括到您的项目

菜单项目 - >编辑项目设置 搜索设置 “标题搜索路径” 添加新的搜索路径“$ {} SDKROOT的/ usr /有/ libxml2" 的 启用递归选项 - 添加libxml2库到你的项目

菜单项目 - >编辑项目设置 搜索设置‘其它链接器标记’ 添加新的搜索旗“-lxml2” - 从hpple得到下面的源代码文件的将它们添加到您的项目:

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

代码示例

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