2014-05-23 136 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); 
    }