XML请求数据和解析

前端之家收集整理的这篇文章主要介绍了XML请求数据和解析前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

对JSON习以为常之后,使用XML多少有点不习惯。其实二者差不多。这里讲述一下iOS自带NSXMLParserDelegate的使用。


1.头文件中声名协议<NSXMLParserDelegate>,定义两个方法-(void)postRequest 和 -(void)startParser:(NSData*)_data


2.源文件中实现,代码如下:

  1. -(void)postRequest
  2. {
  3. //preparrequest
  4. NSString*urlString=[NSStringstringWithFormat:@"http://122.113.26.11/golive/api/loadRegionList.do"];
  5. NSMutableURLRequest*request=[[NSMutableURLRequestalloc]init];
  6. [requestsetURL:[NSURLURLWithString:urlString]];
  7. [requestsetHTTPMethod:@"POST"];
  8. //setheaders设置contentType和HTTPHeaderField
  9. NSString*contentType=[NSStringstringWithFormat:@"text/xml"];
  10. addValue:contentTypeforHTTPHeaderField:@"Content-Type"];
  11. //createthebody在此处拼接XML字段,将每段尖括号的内容拆开拼接起来
  12. NSMutableData*postBody=[NSMutableDatadata];
  13. //[postBodyappendData:[[NSStringstringWithFormat:@"<xml>"]dataUsingEncoding:NSUTF8StringEncoding]];
  14. [postBodyappendData:[[NSStringstringWithFormat:@"<?xmlversion=\"1.0\"encoding=\"utf-8\"?>"]dataUsingEncoding:NSUTF8StringEncoding]];
  15. [postBodystringWithFormat:@"<requestwebsite=\"http://XXXXXXXXXXXXXX\">"]dataUsingEncoding:NSUTF8StringEncoding]];
  16. stringWithFormat:@"<devicedevmodel=\"\"dnum=\"\"didtoken=\"\"dver=\"\"/>"]stringWithFormat:@"<livemac=\"XXXXX\"deviceid=\"\"livetoken=\"\"livever=\"\"/>"]dataUsingEncoding:NSUTF8StringEncoding]];
  17. stringWithFormat:@"</parameter>"]stringWithFormat:@"</request>"]//post
  18. setHTTPBody:postBody];
  19. //getresponse
  20. NSHTTPURLResponse*urlResponse=nil;
  21. NSError*error=[[NSErrorinit];
  22. self.responseData=[NSURLConnectionsendSynchronousRequest:requestreturningResponse:&urlResponseerror:&error];
  23. NSString*result=[[NSStringinitWithData:responseDataencoding:NSUTF8StringEncoding];
  24. NSLog(@"regionResponseCode:%ld",(long)[urlResponsestatusCode]);
  25. if([urlResponsestatusCode]>=200&&[urlResponsestatusCode]<300){
  26. NSLog(@"regionResponse:%@",result);
  27. //这里获取到服务器返回的内容
  28. }
  29. NSString*aStr;
  30. aStr=[[NSStringinitWithData:postBodyencoding:NSASCIIStringEncoding];
  31. NSLog(@"reg==========%@",aStr);
  32. }
  33. //自定义一个PraxMovie方法,参数时NSDate型的数据,用来传递接受到的数据
  34. -(void)startParser:(NSData*)data
  35. {
  36. //NSXMLParser解析xml格式的数据,在这里初始化,并赋值
  37. NSXMLParser*parser=[[NSXMLParseralloc]initWithData:data];
  38. parser.delegate=self;
  39. [parserparse];
  40. NSLog(@"解析完成。。。");
  41. //文档出错的时候触发
  42. void)parser:(NSXMLParser*)parserparseErrorOccurred:(NSError*)parseError
  43. NSLog(@"%@",parseError);
  44. //遇到一个开始标签时候触发
  45. didStartElement:(NSString*)elementName
  46. namespaceURI:(NSString*)namespaceURI
  47. qualifiedName:(NSString*)qualifiedName
  48. attributes:(NSDictionary*)attributeDict
  49. self.currentTagName=elementName;
  50. //region是需要解析的内容所对应的字段
  51. if([currentTagNameisEqualToString:@"region"]){
  52. //name和code是需要解析的内容所对应的字段
  53. [self.nameaddObject:[attributeDictobjectForKey:@"name"]];
  54. [self.codeobjectForKey:@"code"]];
  55. self.regionDicsetObject:[attributeDictobjectForKey:@"code"]forKey:[attributeDict NSLog(@"language:%@;code:%@",[attributeDictobjectForKey:@"name"],0); background-color:inherit">objectForKey:@"code"]);
  56. //遇到结束标签时候出发
  57. -(didEndElement:(NSString*)elementName
  58. NSString*)namespaceURI
  59. NSString*)qName;
  60. self.currentTagName= }

这样就OK了,成功获取后台数据并解析成所需要的内容 原文链接:https://www.f2er.com/xml/297872.html

猜你在找的XML相关文章