首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >解析位于<img>提要项的<description></description>中的rss2标记

解析位于<img>提要项的<description></description>中的rss2标记
EN

Stack Overflow用户
提问于 2011-10-12 05:20:55
回答 1查看 770关注 0票数 1

我试图实现的是使用RSS2提要项的src属性。提要的xml项如下所示:

代码语言:javascript
复制
<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>上失败了。以下是代码:

代码语言:javascript
复制
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,的描述时,我会得到以下内容:

代码语言:javascript
复制
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 属性解析它,或者我必须使用正则表达式来解析它?

所有的建议都非常欢迎。

EN

回答 1

Stack Overflow用户

发布于 2012-02-10 11:24:36

我遇到了同样的问题并找到了解决办法。应该是这样的:

代码语言:javascript
复制
NSArray *imgs = [articleDesc nodesForXPath:@"//img/@src" error:nil];
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7735587

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档