解析
位于 内的标签RSS2 提要项目的
我试图完成的是使用 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>
我当前使用的代码可以很好地解析
、
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>"}
Is it possible to parse the src
属性 使用“快速”GDataXML
或者我必须使用正则表达式来完成?
非常欢迎所有建议。
What I try to accomplish is get the src attribute of an RSS2 feed item using GDataXML
. The feed's item xml is like this:
<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>
The code I am currently using, parses pretty fine the <description>
, <title>
, <link>
; and <pubdate>
; but fails on <img>
;. Here is the code:
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];
}
}
When I print to console the description of articleDesc
, I get the following:
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>"}
Is it possible to parse the src
attribute using the "fast" GDataXML
or I'll have to do it using regular expressions?
All suggestions are very welcome.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我遇到了同样的问题并找到了解决方案。应该是这样的:
I ran into the same problem and found the solution. It should be like this: