标签:ios afnetworking xml
因为AFNetworking2.0中,对于响应返回的xml格式没有做专门的解析处理,因此需要开发者自己来做处理。在笔者的项目中,使用了一个叫AFGDataXMLRequestOperation的第三方类库来统一处理。
该库的xml解析逻辑基于GDataXML-HTML类库。不过类库处理逻辑应该还是基于AFNetworking 1.0版本,有两处需要做适配修改,不然调试不过:
1、在responseXMLDocument方法中,将responseXMLDocument的初始化方法做了调整;
2、在setCompletionBlockWithSuccess方法中,将失败Queue self.failureCallbackQueue与成功Queueself.successCallbackQueue 统一修改为self.completionQueue
AFGDataXMLRequestOperation下载地址:
https://github.com/graetzer/AFGDataXMLRequestOperation
GDataXML-HTML库下载地址:
https://github.com/graetzer/GDataXML-HTML
NSURLRequest * request = [[NSURLRequestalloc]initWithURL:[[NSURLalloc]initWithString:@"http://rss.sina.com.cn/tech/index.shtml"]];
AFGDataXMLRequestOperation * xmlOpera = [AFGDataXMLRequestOperationXMLDocumentRequestOperationWithRequest:requestsuccess: ^(NSURLRequest * request,NSHTTPURLResponse * response,GDataXMLDocument * xmlDoc) {
GDataXMLElement * elem = [[[xmlDocrootElement]elementsForName:@"REC"]objectAtIndex:0];
NSLog(@"XMLDocumentFirst Node Name: %@", [elemattributeForName:@"name"]);
} failure:^(NSURLRequest *request,NSHTTPURLResponse * response,NSError * error, GDataXMLDocument * xmlDoc){
NSLog (@"requestFlagshipStoreDataError: %@", error);
}];
[xmlOpera start];
因为AFNetWorking的回调中返回的responseObject是一个NSXMLParser类,用于启动并进行XML解析,所以我们也可以自己实现NSXMLParserDelegate接口,来自己实现xml解析。
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManagermanager];
AFXMLParserResponseSerializer * responseSer = [AFXMLParserResponseSerializernew];
[manager setResponseSerializer:responseSer];
NSDictionary *dict =@{@"format":@"xml"};
[manager GET:@"http://rss.sina.com.cn/tech/index.shtml" parameters:dictsuccess:^(AFHTTPRequestOperation *operation,id responseObject){
NSXMLParser * parser = (NSXMLParser *)responseObject;
parser.delegate =self;
[parser setShouldProcessNamespaces:YES];
//此法基本可行,但是处理麻烦
[parser parse];
LOGDEBUG(@"requestFlagshipStoreData xml:%@",parser);
} failure:^(AFHTTPRequestOperation *operation,NSError *error) {
LOGDEBUG(@"requestFlagshipStoreData Error:%@",error);
}];
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#pragma mark - NSXMLParserDelegate委托处理
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//在文档开始的时候触发
-(void)parserDidStartDocument:(NSXMLParser *)parser{
LOGDEBUG(@"解析开始!");
}
//解析起始标记
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementNamenamespaceURI:(NSString *)namespaceURIqualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{
LOGDEBUG(@"标记:%@",elementName);
}
//解析文本节点
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
LOGDEBUG(@"值:%@",string);
}
//解析结束标记
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementNamenamespaceURI:(NSString *)namespaceURIqualifiedName:(NSString *)qName{
LOGDEBUG(@"结束标记:%@",elementName);
}
//文档结束时触发
-(void) parserDidEndDocument:(NSXMLParser *)parser{
LOGDEBUG(@"解析结束!");
}
【IOS】AFNetworking 2.0中XML请求处理专题
标签:ios afnetworking xml
原文地址:http://blog.csdn.net/junbaozi/article/details/42127143