0

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

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 标签获取文章的标题。我解析的大多数 xml 都包含 mp3,无论是在“guid”还是“link”标签中。但是,我正在使用的当前提要如下所示:

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

如何将附件中的 url 传递到 NSString 中?

4

1 回答 1

2

应该这样[[item attributeForName: @"url"] stringValue]做。根据enclosure元素在 XML 树中的实际位置,您当然可能需要使用另一个元素来代替channel

答案是

[[[[item elementsForName: @"enclosure"] lastObject] attributeForName: @"url"] stringValue]
于 2012-07-14T20:44:07.723 回答