parsing位于rss2供稿项目的<description> </ description>内的<img>标签
我试图完成的是使用GDataXML
获取RSS2订阅源项目的src属性。 Feed的项目xml是这样的:
<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>
我正在使用的代码,很好地parsing<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><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/>RIM has confirmed that…</description>"}
是否有可能使用“快速” GDataXML
parsingsrc
属性 ,或者我将不得不使用正则expression式来做到这一点?
所有的build议都非常欢迎。
我遇到了同样的问题,find了解决办法。 应该是这样的:
NSArray *imgs = [articleDesc nodesForXPath:@"//img/@src" error:nil];