iPhone Parsing Enclosure url标签?

时间:2012-07-12 18:32:50

标签: iphone rss xml-parsing gdata

我正在处理我正在处理的博客的问题。我用来解析的方法是:

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

编辑:XML看起来像:

<item>
    <title>Your Direction Determines Your Destination</title>
    <link>http://treymorgan.podbean.com/2012/06/05/your-direction-determines-your-destination/</link>

    <comments>http://treymorgan.podbean.com/2012/06/05/your-direction-determines-your-destination/#comments</comments>
    <pubDate>Tue, 05 Jun 2012 17:57:07 +0000</pubDate>
    <dc:creator>treymorgan</dc:creator>

<category></category>
    <guid isPermaLink="false">http://treymorgan.podbean.com/2012/06/05/your-direction-determines-your-destination/</guid>
    <description><![CDATA[Part 1 of the series &#8230; Road Trip

]]></description>
        <content:encoded><![CDATA[<p>Part 1 of the series &#8230; Road Trip
</p>
]]></content:encoded>

        <wfw:commentRss>http://treymorgan.podbean.com/2012/06/05/your-direction-determines-your-destination/feed/</wfw:commentRss>
        <enclosure url="http://treymorgan.podbean.com/mf/feed/x52488/DirectionDeterminesyourDestination.mp3" length="32015229" type="audio/mpeg"/>
</item>

这样我就可以从XML中的Title标签中获取文章的标题。我解析过的大多数xmls都包含了mp3,无论是在“guid”还是“link”标签中。但是,我正在使用的当前Feed,如下所示:

<enclosure url="http://treymorgan.podbean.com/mf/feed/bf8mvq/Accommoditions.mp3" length="29865594" type="audio/mpeg"/>

如何将机箱中的URL传递给NSString?

1 个答案:

答案 0 :(得分:2)

[[item attributeForName: @"url"] stringValue]之类的东西应该这样做。根据XML树中enclosure元素的实际位置,您当然可能需要使用另一个元素而不是channel

答案是

[[[[item elementsForName: @"enclosure"] lastObject] attributeForName: @"url"] stringValue]