解析< img>位于< description></description>中的标记rss2供稿商品 [英] Parse <img> tag that lies within the <description></description> of an rss2 feed item

查看:100
本文介绍了解析< img>位于< description></description>中的标记rss2供稿商品的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图完成的工作是使用GDataXML获取RSS2提要项的src属性.提要的项目xml如下所示:

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>

我当前使用的代码可以很好地解析<description><title><link>;和<pubdate>;但在<img>;上失败.这是代码:

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];
    }      
}

在打印控制台 articleDesc 的描述时,我得到以下信息:

When I print to console the description of articleDesc, I get the following:

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>"}

是否可以使用快速" GDataXML来解析 src属性,还是必须使用正则表达式进行解析?

Is it possible to parse the src attribute using the "fast" GDataXML or I'll have to do it using regular expressions?

非常欢迎所有建议.

推荐答案

我遇到了同样的问题,并找到了解决方案.应该是这样的:

I ran into the same problem and found the solution. It should be like this:

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

这篇关于解析&lt; img&gt;位于&lt; description&gt;&lt;/description&gt;中的标记rss2供稿商品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆