开发者

Parse <img> tag that lies within the <description></description> of an rss2 feed item

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 *img开发者_开发技巧s = [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>&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>"}

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.


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

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜