1

私が達成しようとしているのは、 を使用して RSS2 フィード項目の src 属性を取得することですGDataXML。フィードのアイテム 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>

私が現在使用しているコードは<description>、 , <title>, <link>;をかなり細かく解析します。そして<pubdate>; しかし失敗し<img>ます;。コードは次のとおりです。

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…&lt;/description>"}

「高速」を使用してsrc属性を解析することは可能GDataXMLですか、それとも正規表現を使用して行う必要がありますか?

すべての提案は大歓迎です。

4

1 に答える 1

2

私は同じ問題に遭遇し、解決策を見つけました。次のようになります。

NSArray *imgs = [articleDesc nodesForXPath:@"//img/@src" error:nil];
于 2012-02-10T11:24:36.370 に答える