【问题标题】:Parse <img> tag that lies within the <description></description> of an rss2 feed item解析位于 rss2 提要项的 <description></description> 内的 <img> 标记
【发布时间】:2011-12-05 20:11:42
【问题描述】:

我尝试完成的是使用GDataXML 获取 RSS2 提要项的 src 属性。 feed 的 item 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>

我目前使用的代码可以很好地解析&lt;description&gt;&lt;title&gt;&lt;link&gt;;和&lt;pubdate&gt;;但在&lt;img&gt; 上失败;。代码如下:

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

是否可以使用“快速”GDataXML 解析 src 属性,或者我必须使用正则表达式来解析?

非常欢迎所有建议。

【问题讨论】:

    标签: objective-c ios xml-parsing for-xml-path gdataxml


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-09-27
      • 1970-01-01
      • 2012-08-04
      • 1970-01-01
      • 1970-01-01
      • 2014-12-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多