来自请求的原始数据始终在responseData
操作类的属性中可用。以下是您将如何展示它NSLog
:
__block AFXMLRequestOperation *operation = [AFXMLRequestOperation XMLParserRequestOperationWithRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://legalindexes.indoff.com/sitemap.xml"]] success:^(NSURLRequest *request, NSHTTPURLResponse *response, NSXMLParser *XMLParser) {
NSLog(@"Parser: %@", XMLParser);
NSLog(@"Raw XML Data: %@", [[NSString alloc] initWithData:operation.responseData encoding:NSUTF8StringEncoding]);
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, NSXMLParser *XMLParser) {
NSLog(@"Failure!");
}];
[operation start];
也就是说,标准AFXMLRequestOperation
类使用NSXMLParser
起来很痛苦。如果您的有效负载不是太大并且性能不是问题,我建议您使用 Mattt 的新AFKissXMLRequestOperation类,它将为您进行解析并公开一个更友好的NSXMLDocument兼容对象:
__block AFKissXMLRequestOperation *operation = [AFKissXMLRequestOperation XMLDocumentRequestOperationWithRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://legalindexes.indoff.com/sitemap.xml"]] success:^(NSURLRequest *request, NSHTTPURLResponse *response, DDXMLDocument *XMLDocument) {
NSLog(@"XMLDocument: %@", XMLDocument);
NSLog(@"Raw XML Data: %@", [[NSString alloc] initWithData:operation.responseData encoding:NSUTF8StringEncoding]);
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, DDXMLDocument *XMLDocument) {
NSLog(@"Failure!");
}];
[operation start];