对JSON习以为常之后,使用XML多少有点不习惯。其实二者差不多。这里讲述一下iOS自带的NSXMLParserDelegate的使用。
1.头文件中声名协议<NSXMLParserDelegate>,定义两个方法:-(void)postRequest 和 -(void)startParser:(NSData*)_data
- -(void)postRequest
- {
- //preparrequest
- NSString*urlString=[NSStringstringWithFormat:@"http://122.113.26.11/golive/api/loadRegionList.do"];
- NSMutableURLRequest*request=[[NSMutableURLRequestalloc]init];
- [requestsetURL:[NSURLURLWithString:urlString]];
- [requestsetHTTPMethod:@"POST"];
- //setheaders设置contentType和HTTPHeaderField
- NSString*contentType=[NSStringstringWithFormat:@"text/xml"];
- addValue:contentTypeforHTTPHeaderField:@"Content-Type"];
- //createthebody在此处拼接XML字段,将每段尖括号的内容拆开拼接起来
- NSMutableData*postBody=[NSMutableDatadata];
- //[postBodyappendData:[[NSStringstringWithFormat:@"<xml>"]dataUsingEncoding:NSUTF8StringEncoding]];
- [postBodyappendData:[[NSStringstringWithFormat:@"<?xmlversion=\"1.0\"encoding=\"utf-8\"?>"]dataUsingEncoding:NSUTF8StringEncoding]];
- [postBodystringWithFormat:@"<requestwebsite=\"http://XXXXXXXXXXXXXX\">"]dataUsingEncoding:NSUTF8StringEncoding]];
- stringWithFormat:@"<devicedevmodel=\"\"dnum=\"\"didtoken=\"\"dver=\"\"/>"]stringWithFormat:@"<livemac=\"XXXXX\"deviceid=\"\"livetoken=\"\"livever=\"\"/>"]dataUsingEncoding:NSUTF8StringEncoding]];
- stringWithFormat:@"</parameter>"]stringWithFormat:@"</request>"]//post
- setHTTPBody:postBody];
- //getresponse
- NSHTTPURLResponse*urlResponse=nil;
- NSError*error=[[NSErrorinit];
- self.responseData=[NSURLConnectionsendSynchronousRequest:requestreturningResponse:&urlResponseerror:&error];
- NSString*result=[[NSStringinitWithData:responseDataencoding:NSUTF8StringEncoding];
- NSLog(@"regionResponseCode:%ld",(long)[urlResponsestatusCode]);
- if([urlResponsestatusCode]>=200&&[urlResponsestatusCode]<300){
- NSLog(@"regionResponse:%@",result);
- //这里获取到服务器返回的内容
- }
- NSString*aStr;
- aStr=[[NSStringinitWithData:postBodyencoding:NSASCIIStringEncoding];
- NSLog(@"reg==========%@",aStr);
- }
- //自定义一个PraxMovie方法,参数时NSDate型的数据,用来传递接受到的数据
- -(void)startParser:(NSData*)data
- {
- //NSXMLParser解析xml格式的数据,在这里初始化,并赋值
- NSXMLParser*parser=[[NSXMLParseralloc]initWithData:data];
- parser.delegate=self;
- [parserparse];
- NSLog(@"解析完成。。。");
- //文档出错的时候触发
- void)parser:(NSXMLParser*)parserparseErrorOccurred:(NSError*)parseError
- NSLog(@"%@",parseError);
- //遇到一个开始标签时候触发
- didStartElement:(NSString*)elementName
- namespaceURI:(NSString*)namespaceURI
- qualifiedName:(NSString*)qualifiedName
- attributes:(NSDictionary*)attributeDict
- self.currentTagName=elementName;
- //region是需要解析的内容所对应的字段
- if([currentTagNameisEqualToString:@"region"]){
- //name和code是需要解析的内容所对应的字段
- [self.nameaddObject:[attributeDictobjectForKey:@"name"]];
- [self.codeobjectForKey:@"code"]];
- self.regionDicsetObject:[attributeDictobjectForKey:@"code"]forKey:[attributeDict NSLog(@"language:%@;code:%@",[attributeDictobjectForKey:@"name"],0); background-color:inherit">objectForKey:@"code"]);
- //遇到结束标签时候出发
- -(didEndElement:(NSString*)elementName
- NSString*)namespaceURI
- NSString*)qName;
- self.currentTagName= }
这样就OK了,成功获取到后台数据并解析成所需要的内容。 原文链接:https://www.f2er.com/xml/297872.html