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

…衆ロ難τιáo~ 提交于 2019-12-08 08:50:54

问题


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>&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.


回答1:


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

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


来源:https://stackoverflow.com/questions/7735587/parse-img-tag-that-lies-within-the-description-description-of-an-rss2-feed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!