1.1? ? XML請求處理
1.1.1 ?xml處理機制
因為AFNetworking2.0中宛裕,對于響應返回的xml格式?jīng)]有做專門的解析處理田弥,因此需要開發(fā)者自己來做處理涛目。在筆者的項目中赞季,使用了一個叫AFGDataXMLRequestOperation的第三方類庫來統(tǒng)一處理现恼。
該庫的xml解析邏輯基于GDataXML-HTML類庫肃续。不過類庫處理邏輯應該還是基于AFNetworking 1.0版本,有兩處需要做適配修改叉袍,不然調(diào)試不過:
1始锚、在responseXMLDocument方法中,將responseXMLDocument的初始化方法做了調(diào)整喳逛;
2瞧捌、在setCompletionBlockWithSuccess方法中,將失敗Queue self.failureCallbackQueue與成功Queueself.successCallbackQueue 統(tǒng)一修改為self.completionQueue
AFGDataXMLRequestOperation下載地址:
https://github.com/graetzer/AFGDataXMLRequestOperation
GDataXML-HTML庫下載地址:
https://github.com/graetzer/GDataXML-HTML
1.1.2 范例代碼
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);
}];
[xmlOperastart];