获取完整的XML下载数据 [英] Get the entire XML downloaded data

查看:172
本文介绍了获取完整的XML下载数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个过时的应用程序,用于下载XML文档并将其解析到iPhone应用程序上,为此我使用了 NSURLConnection

I have an outdated application which use to download an XML document and parse it on the iPhone app, I used the NSURLConnection for that purpose:

-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    //NSLog(@"Response :%@",response);
    responseData = [[NSMutableString alloc] init];
}

-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    NSString *str = [[NSString alloc] initWithData:data 
                                          encoding:NSASCIIStringEncoding];

    [responseData appendString:str];
    [str release];
}

-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{

    NSLog(@"DATA : %@",responseData);   

    if (responseData != nil) {

        [self startParsing:responseData];//Parse the data
        [responseData release];
    }   
}   

由于开始使用 NSXMLParserDelegate AFXMLRequestOperation ,我无法找出正确获取xml数据的方法:

Since moving to use NSXMLParserDelegate with AFXMLRequestOperation, I cannot figure out a way to get xml data properly:

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{

    [responseData appendString:elementName];
    [responseData appendString:namespaceURI];
    [responseData appendString:qName];

}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
    [responseData appendString:string];

}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{
    [responseData appendString:elementName];
    [responseData appendString:namespaceURI];
    [responseData appendString:qName];


}
-(void) parserDidEndDocument:(NSXMLParser *)parser{
    [SVProgressHUD showSuccessWithStatus:@"Downloading completed"];

        NSLog(@"DATA : %@",responseData);//not properly appended, tags delimeters are missing 

        if (responseData != nil) {

            [self startParsing:responseData];
            [responseData release];
        }


}

如何附加 responseData 可变字符串中从服务器收到的所有数据?在完成下载后,我调试了收到的数据,并且xml缺少标记分隔符<> 。我想我可能缺少获取xml数据的方法。

How to append all the data received from the server in the responseData mutable string ? I debugged the data received after finishing downloading and the xml is missing tags delimeters <>. I think I ma missing the way to get the xml data.

PS:请注意,以 NSMutableString 对象。

@Fermi

我按照您的建议使用了 AFURLConnectionOperation ,它可以很好地实现我的目的,但是我注意到委托方法没有捕获我收到的数据,相反,我可以得到完成块中的数据:

I used AFURLConnectionOperation as you recommended, it works fine with my purpose, but I noticed that my received data is not catched by the delegate methods, instead I can get the data in a completion block:

NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:API_URL]];

AFURLConnectionOperation *operation = [[AFURLConnectionOperation alloc] initWithRequest:request];
operation.completionBlock = ^{
    NSLog(@"Complete: %@",operation.responseString);//responseString is my data


};
[operation start];
[SVProgressHUD showWithStatus:@"Downloading files"];

wo因为未调用 NSURLConnection 委托方法,如何管理故障等?谢谢。

wo since NSURLConnection delegate methods are not called, how can I manage failure, etc? Thanx.

推荐答案

AFXMLRequestOperation 专门用于返回您 NSXMLDocument 实例,而不是原始XML字符串。

AFXMLRequestOperation is explicitly intended to be used to return you an NSXMLDocument instance, NOT a raw XML string.

如果要使用XML字符串,请使用 AFURLConnectionOperation 并以与 NSURLConnection 相同的方式构建 NSMutableString

If you want the XML string use AFURLConnectionOperation and build the NSMutableString the same way you do with NSURLConnection.

这篇关于获取完整的XML下载数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆