iPhone開發筆記——webservice解析xml
阿新 • • 發佈:2018-12-25
給你一個我做過的案例吧是關於一個webservice的解析的關鍵市解析xml檔案,在蘋果底下沒有現成的類將xml檔案解析成樹狀的類,自己按照幫助文件的案例推敲吧!
#import "QQViewController.h" @implementation QQViewController @synthesize qqCodeText; @synthesize qqStatusLabel; @synthesize webData; @synthesize soapResults; @synthesize xmlParser; @synthesize recordResults; @synthesize activityIndicatorView; //處理文字輸入完畢鍵盤的隱藏 或者在輸入完畢按回車時直接進行查詢 -(IBAction)textDidEndExit:(id)sender{ //[qqCodeText resignFirstResponder]; [sender resignFirstResponder]; } - (void)getQQStatus{ recordResults=NO; //soap request message NSString *soapMessage=[NSString stringWithFormat: @"<?xml version="1.0"encoding="utf-8"?>n" "<soap:Envelopexmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:xsd="http://www.w3.org/2001/XMLSchema"xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">n" "<soap:Body>" "<qqCheckOnlinexmlns="http://WebXml.com.cn/">" "<qqCode>%@</qqCode>" "</qqCheckOnline>" "</soap:Body>" "</soap:Envelope>",qqCodeText.text]; //請求地址 NSURL *url=[NSURLURLWithString:@"http://webservice.webxml.com.cn/webservices/qqOnlineWebService.asmx"]; NSMutableURLRequest *theRequest=[NSMutableURLRequestrequestWithURL:url]; NSString *msgLegth=[NSString stringWithFormat:@"%d",[soapMessagelength]]; [theRequest addValue:@"text/xml; charset=utf-8"forHTTPHeaderField:@"Content-Type"]; [theRequest addValue:@"http://WebXml.com.cn/qqCheckOnline"forHTTPHeaderField:@"SOAPAction"]; [theRequest addValue:msgLegthforHTTPHeaderField:@"Content-Length"]; [theRequest setHTTPMethod:@"POST"]; [theRequest setHTTPBody:[soapMessagedataUsingEncoding:NSUTF8StringEncoding ]]; //request NSURLConnection *theConnection=[[NSURLConnection alloc]initWithRequest:theRequest delegate:self]; //connection if(theConnection){ webData=[[NSMutableData data]retain]; }else{ NSLog(@"theConnection is NULL"); } } -(IBAction)selectStatus{ //
[email protected]"Getting time ..."; //等待介面 [activityIndicatorView startAnimating]; [qqCodeText resignFirstResponder];//為什麼在這裡釋放 [self getQQStatus]; } - (void)didReceiveMemoryWarning { // Releases the view if it doesn't have a superview. [super didReceiveMemoryWarning]; // Release any cached data, images, etc that aren't in use. } - (void)viewDidUnload { // Release any retained subviews of the main view. // e.g. self.myOutlet = nil; self.qqCodeText=nil; self.qqStatusLabel=nil; [super viewDidUnload]; } -(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{ // Return YES for supported orientations return (interfaceOrientation ==UIInterfaceOrientationPortrait); } - (void)dealloc { [qqCodeText release]; [qqStatusLabel release]; [super dealloc]; } //接受到資料 -(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{ [webData appendData:data]; NSLog(@"connection didReceiveData:2"); } //沒有接受到資料 -(void)connection:(NSURLConnection *)connectiondidReceiveResponse:(NSURLResponse *)response{ NSLog(@"webdata length is :%d",[webData length]); [webData setLength:0]; NSLog(@"connection:didReceiveResponse:1"); } // -(void)connection:(NSURLConnection *)connectiondidFailWithError:(NSError *)error{ NSLog(@"ERROR with theConnection"); [connection release]; [webData release]; } -(void)connectionDidFinishLoading:(NSURLConnection*)connection{ NSString *theXML=[[NSString alloc] initWithBytes:[webData mutableBytes] length:[webData length] encoding:NSUTF8StringEncoding]; [theXML release]; if(xmlParser){ [xmlParser release]; } xmlParser=[[NSXMLParser alloc] initWithData:webData]; [xmlParser setDelegate:self]; [xmlParser setShouldResolveExternalEntities:YES]; [xmlParser parse]; [connection release]; } -(void)parser:(NSXMLParser *)parser didStartElement:(NSString*)elementName namespaceURI:(NSString *)namespaceURIqualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{ if([elementName isEqualToString:@"qqCheckOnlineResult"]){ if(!soapResults){ soapResults=[[NSMutableString alloc] init]; } recordResults=YES; } } -(void)parser:(NSXMLParser *)parser foundCharacters:(NSString*)string{ if(recordResults){ [soapResults appendString:string]; } } //在這裡接收到返回的資料 -(void)parser:(NSXMLParser *)parser didEndElement:(NSString*)elementName namespaceURI:(NSString *)namespaceURIqualifiedName:(NSString *)qName{ if([elementName isEqual:@"qqCheckOnlineResult"]){ recordResults=FALSE; [activityIndicatorView stopAnimating]; if([soapResults isEqualToString:@"Y"]){ qqStatusLabel.text=[[[NSString init] stringWithFormat:@"%@ 狀態是:",qqCodeText.text] stringByAppendingString:@"線上"]; }else if([soapResults isEqualToString:@"N"]){ qqStatusLabel.text=[[[NSString init] stringWithFormat:@"%@ 狀態是:",qqCodeText.text] stringByAppendingString:@"離線"]; }else if([soapResults isEqualToString:@"E"]){ qqStatusLabel.text=[[[NSString init] stringWithFormat:@"%@ 狀態是:",qqCodeText.text] stringByAppendingString:@"QQ號碼錯誤"]; }else if([soapResults isEqualToString:@"A"]){ qqStatusLabel.text=[[[NSString init] stringWithFormat:@"%@ 狀態是:",qqCodeText.text] stringByAppendingString:@"商業使用者驗證失敗"]; }else if([soapResults isEqualToString:@"V"]){ qqStatusLabel.text=[[[NSString init] stringWithFormat:@"%@ 狀態是:",qqCodeText.text] stringByAppendingString:@"免費使用者超過數量"]; } [soapResults release]; soapResults=nil; } } -(void)parserDidStartDocument:(NSXMLParser *)parser{ //解析開始 //[activityIndicatorView ]; } -(void)parserDidEndDocument:(NSXMLParser *)parser{ //解析完成 } -(void)viewDidLoad{ [activityIndicatorView stopAnimating]; [activityIndicatorView hidesWhenStopped]; [super viewDidLoad]; } @end