1

我正在为我的一个朋友的播客创建一个应用程序。该应用程序有一个带有其 URL 的 rss xml 文件。在这里查看之后,我决定使用 KissXML 作为它的文档,但我似乎无法让 DDXMLDocument 正常工作。我创建了一个与播客的 rss 提要的连接,其 URL 格式为

NSString *podcastURL = [NSString stringWithFormat:@"http://podcast.website.com/rss/"];
NSURL *requestURL = [NSURL URLWithString:podcastURL];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:requestURL];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self]

然后,在委托方法中

-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data

我正在尝试使用接收到的数据来创建 DDXMLDocument。在使用...创建字符串后,我尝试了 initWithData 和 initWithString

NSError *error = nil;
NSString *response = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

我 NSLog 这个字符串来查看它的内容,对我来说它看起来就像一个完美的 xml 文档。我创建 XMLDoc

DDXMLDocument *responseDoc = [[DDXMLDocument alloc] initWithXMLString:response options:0 error:&error];

但是当我尝试检查这个新的 XMLDoc 时,我得到的只是 NULL。谁能看看我做错了什么?XML 字符串响应对我来说似乎是有效的,但我想我真的不知道。谢谢你的帮助!

编辑:我想我应该添加一些我收到的 XML,以防有人发现我无法找到的问题。不过,播客有 100 多集,每集都有长度、描述、标题、iTunes 的一堆东西等所有内容。这是 XML 开头的样子,其中包括一集(虽然没有任何真实的播客信息):

<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"         xmlns:cc="http://web.resource.org/cc/" xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" xmlns:media="http://search.yahoo.com/mrss/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
<channel>
    <atom:link href="http://podcast.libsyn.com/rss/" rel="self" type="application/rss+xml"/>
    <title>Podcast</title>
    <pubDate>Mon, 01 Oct 2012 06:18:33 +0000</pubDate>
    <lastBuildDate>Wed, 03 Oct 2012 22:19:37 +0000</lastBuildDate>
    <generator>Libsyn WebEngine 2.0</generator>
    <link>http://podcast.libsyn.com</link>
    <language>en</language>
    <copyright><![CDATA[podcast]]></copyright>
    <docs>http://podcast.libsyn.com</docs>
    <managingEditor>person@hotmail.com (person@hotmail.com)</managingEditor>
    <description><![CDATA[Comedy podcast with People!]]></description>
    <image>
        <url>http://assets.libsyn.com/content/3220867.jpg</url>
        <title>podcast</title>
        <link><![CDATA[http://podcast.libsyn.com]]></link>
    </image>
    <itunes:author>Person1  and Person2</itunes:author>
<itunes:keywords>keywords</itunes:keywords>
<itunes:category text="Comedy"/>
    <itunes:image href="http://assets.libsyn.com/content/3220867.jpg" />
    <itunes:explicit>yes</itunes:explicit>
    <itunes:owner>
        <itunes:name><![CDATA[Person1]]></itunes:name>
        <itunes:email>person@hotmail.com</itunes:email>
    </itunes:owner>
    <itunes:summary><![CDATA[Show Summary]]></itunes:summary>
    <itunes:subtitle><![CDATA[podcast]]></itunes:subtitle>
            <item>
        <title>#123 - Episode123 Title</title>
        <pubDate>Mon, 01 Oct 2012 06:18:33 +0000</pubDate>
        <guid isPermaLink="false"><![CDATA[46f771dfb560aa4d573fe3ebdee86870]]></guid>
        <link><![CDATA[http://podcast.libsyn.com/Episode123_Title]]></link>
        <media:thumbnail url="http://assets.libsyn.com/item/2082832" />
        <description><![CDATA[<p>Description of Episode 123.</p>
<p>Music: Episode 123 Music</p>]]></description>
        <enclosure length="80069010" type="audio/mpeg" url="http://traffic.libsyn.com/podcast/123.mp3" />
        <itunes:duration>01:06:44</itunes:duration>
        <itunes:explicit>no</itunes:explicit>
        <itunes:keywords />
        <itunes:subtitle><![CDATA[Episode 123 Description]]></itunes:subtitle>
                </item>

对不起,如果这太多了,不能在这里发布。此 XML 中包含标题内容和一个项目(剧集)。这看起来像应该与 DDXMLDocument 一起使用的正确代码吗?再次感谢大家。

4

2 回答 2

0

看起来蒂姆·迪恩的评论是正确的。我收到的 XML 字符串非常庞大,以至于我没有注意到它们实际上是在 didReceiveData: 中调用 NSLog 的时间之间分开的。一切都很好,只是无法使用这些部分字符串创建 DDXMLDocument。谢谢!

于 2012-10-04T08:01:33.803 回答
0

好吧,也许有点晚了,但你为什么不使用KissXML 本身的这个例子

-(void)downloadXML{
    AFKissXMLRequestOperation *operation = [AFKissXMLRequestOperation XMLDocumentRequestOperationWithRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://legalindexes.indoff.com/sitemap.xml"]] success:^(NSURLRequest *request, NSHTTPURLResponse *response, DDXMLDocument *XMLDocument) {
      NSLog(@"XMLDocument: %@", XMLDocument);
  } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, DDXMLDocument *XMLDocument) {
      NSLog(@"Failure!");
}];

[operation start];

然后你需要做的就是XMLDocument拿出需要的东西DDXMLElements

于 2012-11-20T12:01:56.387 回答