iPhone开发笔记——webservice解析xml

给你一个我做过的案例吧是关于一个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{
//qqStatusLabel.text=@"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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值