3

私のXMLデータがあります:

    <item>
    <title>The Powerful Academician Able to Reduce Tobacco Hazards</title>
    <link>http://china15min.com/2013/03/26/the-powerful-academician-able-to-reduce-tobacco-hazards/</link>
    <comments>http://china15min.com/2013/03/26/the-powerful-academician-able-to-reduce-tobacco-hazards/#comments</comments>
    <pubDate>Tue, 26 Mar 2013 08:43:37 +0000</pubDate>
    <dc:creator>Panda Walking</dc:creator>
            <category><![CDATA[Economics]]></category>
    <category><![CDATA[Academician of Chinese Engineering Academy]]></category>
    <category><![CDATA[China Tobacco Control Association]]></category>
    <category><![CDATA[China's tobacco industry]]></category>
    <category><![CDATA[CORESTA]]></category>
    <category><![CDATA[low-tar cigarettes in China]]></category>
    <category><![CDATA[signatory country of the World Health Assembly on Tobacco Control Framework Convention]]></category>
    <category><![CDATA[smokers in China]]></category>
    <category><![CDATA[Xie Jian Ping]]></category>
    <category><![CDATA[Yunnan]]></category>

    <guid isPermaLink="false">http://china15min.com/?p=1891</guid>
    <description><![CDATA[During the two sessions, the deputies of NPC are obliged to answer journalists&#8217; questions, which may include special topics besides national affairs.  For example, why is the &#8220;tobacco academician&#8221; able to keep his position unswayed despite strong opposition from various... <a href="http://china15min.com/2013/03/26/the-powerful-academician-able-to-reduce-tobacco-hazards/" class="read-more">Read More &#8250;</a><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=china15min.com&#038;blog=37468365&#038;post=1891&#038;subd=china15min&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
    <wfw:commentRss>http://china15min.com/2013/03/26/the-powerful-academician-able-to-reduce-tobacco-hazards/feed/</wfw:commentRss>
    <slash:comments>0</slash:comments>

    <media:content url="http://1.gravatar.com/avatar/ad06eed181b094ac3022d4507d38c2b7?s=96&#38;d=http%3A%2F%2F1.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96&#38;r=G" medium="image">
        <media:title type="html">china15min</media:title>
    </media:content>

    <media:content url="http://china15min.files.wordpress.com/2013/03/xiejinping.jpg" medium="image">
        <media:title type="html">xiejinping</media:title>
    </media:content>
</item>

このメソッドで、「link」、「title」、および「pubDate」ノードを取得できます。

- (void)parseRss:(GDataXMLElement *)rootElement entries:(NSMutableArray *)entries {

    NSArray *channels = [rootElement elementsForName:@"channel"];
    for (GDataXMLElement *channel in channels) {
    NSString *blogTitle = [channel valueForChild:@"title"];
    NSArray *items = [channel elementsForName:@"item"];
    for (GDataXMLElement *item in items) {
        NSString *articleTitle = [item valueForChild:@"title"];
        NSString *articleUrl = [item valueForChild:@"link"];
        NSString *articleDateString = [item valueForChild:@"pubDate"];
        NSDate *articleDate = [NSDate dateFromInternetDateTimeString:articleDateString formatHint:DateFormatHintRFC822];
        BQWRSSEntry *entry = [[BQWRSSEntry alloc] initWithBlogTitle:blogTitle
                                                  articleTitle:articleTitle
                                                    articleUrl:articleUrl
                                                   articleDate:articleDate];
        [entries addObject:entry];

        }
    }
}

そして、同じメソッドを使用して「説明」を取得しましたが、結果の文字列は null です。

「説明」と 2 番目の「media:content url」URL 文字列http://china15min.files.wordpress.com/2013/03/xiejinping.jpg」を取得するにはどうすればよいですか

私を助けてください、どうもありがとうございました。

4

1 に答える 1

2

説明を取得できない理由がわかりません。次のように簡単に値を取得できます。

NSString *description = [item valueForChild:@"description"];

それ以外の場合は、2 番目の「media:content url」URL 文字列を取得するには、次の手順に従います。

NSArray *mediaContents  = [item elementsForName:@"media:content"];
if ([mediaContents count] > 1)
{
    GDataXMLElement *media2 = [mediaContents objectAtIndex:1];
    GDataXMLNode *urlString = [media2 attributeForName:@"url"];
}

完全なコードは次のようになります。

- (void)parseRss:(GDataXMLElement *)rootElement entries:(NSMutableArray *)entries {

    NSArray *channels = [rootElement elementsForName:@"channel"];
    for (GDataXMLElement *channel in channels) {
        NSString *blogTitle = [channel valueForChild:@"title"];
        NSArray *items = [channel elementsForName:@"item"];
        for (GDataXMLElement *item in items) {
            NSString *articleTitle = [item valueForChild:@"title"];
            NSString *articleUrl = [item valueForChild:@"link"];
            NSString *articleDateString = [item valueForChild:@"pubDate"];

            NSString *description   = [item valueForChild:@"description"];
            NSArray *mediaContents  = [item elementsForName:@"media:content"];
            if ([mediaContents count] > 1)
            {
                GDataXMLElement *media2 = [mediaContents objectAtIndex:1];
                GDataXMLNode *urlString = [media2 attributeForName:@"url"];
            }

            BQWRSSEntry *entry = [[BQWRSSEntry alloc] initWithBlogTitle:blogTitle
                                                  articleTitle:articleTitle
                                                    articleUrl:articleUrl
                                                   articleDate:articleDate];
            [entries addObject:entry];
        }
    }
}
于 2013-04-16T12:57:00.663 に答える