Preface:

HTTP is the most common way for a server to communicate data with a client. In general, the data format is JSON, but sometimes XML. JSON formats are parsed, but how do you parse XML?

This article is based on AFNetworking version 3.2.1

References:

  • Stackoverflow.com/questions/3…
  • Stackoverflow.com/questions/2…

To highlight!

In general, when we make an HTTP request, we instantiate an AFHTTPSessionManager object and set some properties:

manager = [AFHTTPSessionManager manager]; manager.requestSerializer = [AFJSONRequestSerializer serializer]; manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html", @"text/plain",nil]; Manager. RequestSerializer. TimeoutInterval = 30.0 f;Copy the code

We didn’t set the responseSerializer property for manager here. The default responseSerializer is AFJSONResponseSerializer. AFJSONResponseSerializer cannot be used. Here’s the key code:

AFXMLParserResponseSerializer *response = [AFXMLParserResponseSerializer serializer];
manager.responseSerializer = response;
Copy the code

There are some online claims that

AFJSONResponseSerializer *response = [AFJSONResponseSerializer serializer];
response.acceptableContentTypes = [NSSet setWithObjects:@"text/xml", nil];
manager.responseSerializer = response;
Copy the code

This usage is not correct! ResponseSerializer type must be AFXMLParserResponseSerializer here, why can appear such solutions, may be because of a simple see error returned is “cannot receive text/XML data type” and guess the answer, It’s not verified; Alternatively, this method is available in earlier versions of AFNetworking.

Use the following

[manager GET:url parameters:dic progress:nil success:^(NSURLSessionDataTask * _Nonnull task, Id _Nullable responseObject) {NSXMLParser * xmlParser = responseObject; [xmlparser setDelegate:self]; [xmlparser parse]; } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {NSLog(@"LenovoID logon failed :%@", [error localizedDescription]);}];Copy the code

XML parsing needs to be implementedNSXMLParserDelegateagreement

  • Objective – C version
#pragma mark - NSXMLParser delegate - (void)parserDidStartDocument:(NSXMLParser *)parser{NSLog(@"XML document starts "); } -(void)parserDidEndDocument:(NSXMLParser *)parser{NSLog(@"XML document ends "); } - (void)parser:(NSXMLParser *)parser foundElementDeclarationWithName:(NSString *)elementName model:(NSString *)model{ NSLog(@"elementName:%@",elementName); }Copy the code
  • Swift version
extension LoginWebViewController: XMLParserDelegate{ func parserDidStartDocument(_ parser: XMLParser) {print("XML document starts ")} func parserDidEndDocument(_ parser: XMLParser) {print("XML document ends ")} func parser(_ parser: XMLParser, didStartElement elementName: String, namespaceURI: String? , qualifiedName qName: String? , Attributes attributeDict: [String: String] = [:]) {print("XML start element :",elementName)} func parser(_ parser: XMLParser, foundCharacters String: string) {print("XML element content :",string)} func parser(_ parser: XMLParser, didEndElement elementName: String, namespaceURI: String? , qualifiedName qName: String?) {print("XML ending element :",elementName)}}Copy the code

NSXMLParser ¶

  • www.theappguruz.com/blog/xmlpar…

AFNetworking is one of the most basic iOS development skills, so I won’t give you a Demo of how to do it. XML parsing is the most important is set AFHTTPSessionManager responseSerializer attribute value of an object as AFXMLParserResponseSerializer object.