首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >iPhone Objective C KissXML XMLDocument String?

iPhone Objective C KissXML XMLDocument String?
EN

Stack Overflow用户
提问于 2012-10-04 07:08:59
回答 2查看 1.1K关注 0票数 1

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

代码语言:javascript
复制
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]

然后,在委托方法中

-(空)连接:(NSURLConnection*)连接数据:(NSData*)didReceiveData

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

代码语言:javascript
复制
NSError *error = nil;
NSString *response = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

我对这个字符串执行NSLog以查看其内容,对我来说,它看起来就像是一个非常好的xml文档。我用来创建XMLDoc

代码语言:javascript
复制
DDXMLDocument *responseDoc = [[DDXMLDocument alloc] initWithXMLString:response options:0 error:&error];

但是当我尝试检查这个新的XMLDoc时,我得到的结果都是空的。有没有人能看到我做错了什么?XML字符串响应在我看来是有效的,但我想我并不是真的知道。谢谢你的帮助!

编辑:我想我应该添加一些我收到的XML,以防有人发现我找不到的问题。然而,这个播客有100多集,每集都有所有的东西,比如长度,描述,标题,一堆iTunes的东西,等等。下面是XML的开头,其中一集包括在内(尽管没有任何真正的播客信息):

代码语言:javascript
复制
<?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一起工作的正确代码吗?再次感谢大家。

EN

回答 2

Stack Overflow用户

发布于 2012-10-04 16:01:34

看起来蒂姆·迪恩的评论是对的。我收到的XML字符串太大了,以至于我没有注意到它们实际上是在didReceiveData:中调用NSLog的时间之间拆分的。一切都很好,只是不能用这些部分字符串创建DDXMLDocument。谢谢!

票数 0
EN

Stack Overflow用户

发布于 2012-11-20 20:01:56

好吧,可能有点晚了,但为什么不从KissXML本身使用this example

代码语言:javascript
复制
-(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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12718163

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档