2011-10-12 76 views
1

的標籤我試圖完成的是使用GDataXML獲取RSS2訂閱源項目的src屬性。飼料的項目XML是這樣的:解析<img>位於<description></ description>的rss2訂閱源項目

<item> 
    <title>BlackBerry EMEA servers crash</title> 
    <link>http://www.mysite.com/?p=672</link> 
    <comments>http://www.mysite.com/?p=672#comments</comments> 
    <pubDate>Mon, 10 Oct 2011 21:11:24 +0000</pubDate> 
    <dc:creator>acreator</dc:creator> 
    <category><![CDATA[Latest News]]></category> 
    <description><![CDATA[<span class="image-rss"><a href="http://www.mysite.com/?p=672"><img title="BlackBerry EMEA servers crash" src="http://www.mysite.com/wp-content/uploads/2011/10/blackberry-thumb-medium-300x187.jpg" alt="BlackBerry EMEA servers crash" width="200" height="124" /></a></span><br/>yada yada yada]]></description> 
</item> 

我目前使用的代碼,解析精緻漂亮的<description><title><link>;和<pubdate>;但在<img>上失敗;。下面是代碼:

NSArray *channels = [rootElement elementsForName:@"channel"]; 
    for (GDataXMLElement *channel in channels) {    

    NSArray *items = [channel elementsForName:@"item"]; 
    for (GDataXMLElement *item in items) { 

     GDataXMLElement *articleDesc = [item elementForChild:@"description"]; 
     NSArray *imgs = [articleDesc nodesForXPath:@"//img[@src]" error:nil]; 

     NSString *articleTitle = [item valueForChild:@"title"]; 
     NSString *url = [item valueForChild:@"link"];    
     NSString *articleDateString = [item valueForChild:@"pubDate"]; 

     Article *entry = [[[Article alloc] initWithTitle:articleTitle 
                url:url 
                date:articleDate] autorelease]; 
     [entries addObject:entry]; 
    }  
} 

當我打印到控制檯的articleDesc的描述,我得到如下:

GDataXMLElement 0x70503b0: {type:1 name:description xml:"<description>&lt;span class="image-rss"&gt;&lt;a href="http://www.mysite.com/?p=672"&gt;&lt;img title="BlackBerry EMEA servers crash" src="http://www.mysite.com/wp-content/uploads/2011/10/blackberry-thumb-medium-300x187.jpg" alt="BlackBerry EMEA servers crash" width="200" height="124" /&gt;&lt;/a&gt;&lt;/span&gt;&lt;br/&gt;RIM has confirmed that…</description>"} 

是否可以使用「快速解析src屬性GDataXML或者我必須使用正則表達式來完成它?

所有的建議都非常歡迎。

回答

2

我遇到了同樣的問題,並找到解決方案。它應該是這樣的:

NSArray *imgs = [articleDesc nodesForXPath:@"//img/@src" error:nil];