iPhone开发笔记——webservice解析xml

来源:互联网 发布:vb调用数据库数值 编辑:程序博客网 时间:2024/06/05 21:49
给你一个我做过的案例吧是关于一个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 messageNSString *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 ]];//requestNSURLConnection *theConnection=[[NSURLConnection alloc]initWithRequest:theRequest delegate:self];//connectionif(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 orientationsreturn (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以下内容转自http://blog.csdn.net/yjh4866/article/details/7170463
系统有提供的XML解析类,NSXMLParser,但是不是很不好用啊,该类只会以协议的方式通知扫描时所遇到的XML元素,而且该XML数据的层次关系也看不出来,刚刚封装了一个XML解析功能,直接可以得到根结点,然后就可以查看子结点了。NSXMLParser+Cobbler.h[plain] view plaincopy    //      //  NSXMLParser+Cobbler.h      //        //      //  Created by yangjianhong-MAC on 11-12-31.      //  Copyright (c) 2011年 __MyCompanyName__. All rights reserved.      //  QQ:18222469      //            #import <Foundation/Foundation.h>                  #pragma mark -      #pragma mark XMLNode            @interface XMLNode : NSObject {      @private                    NSString *_strNodeName;//结点名称          NSDictionary *_dicAttributes;//结点属性          NSMutableArray *_arrayChild;//子结点          NSString *_strNodeValue;//结点值          NSUInteger _nodeDepth;          XMLNode *_nodeParent;//父结点      }            @property (nonatomic, copy) NSString *nodeName;      @property (nonatomic, copy) NSDictionary *nodeAttributesDict;      @property (nonatomic, readonly) NSArray *children;      @property (nonatomic, copy) NSString *nodeValue;      @property (nonatomic, readonly) NSUInteger nodeDepth;      @property (nonatomic, assign) XMLNode *nodeParent;            - (void)clear;            @end                        #pragma mark -      #pragma mark NSXMLParser Cobbler            @interface NSXMLParser (Cobbler)            + (XMLNode *)parseToXMLNode:(NSData *)dataXML;            @end  NSXMLParser+Cobbler.m[plain] view plaincopy    //      //  NSXMLParser+Cobbler.m      //        //      //  Created by yangjianhong-MAC on 11-12-31.      //  Copyright (c) 2011年 __MyCompanyName__. All rights reserved.      //  QQ:18222469      //            #import "NSXMLParser+Cobbler.h"                  #pragma mark -      #pragma mark XMLNode            @implementation XMLNode            @synthesize nodeName = _strNodeName, nodeValue = _strNodeValue;      @synthesize nodeAttributesDict = _dicAttributes, nodeDepth = _nodeDepth;            #pragma mark Override            //20120610增加      - (void)dealloc      {          [_strNodeName release];          [_dicAttributes release];          [_strNodeValue release];          _nodeParent = nil;                    [super dealloc];      }            - (NSArray *)children      {          if (_arrayChild.count > 0) {              return [NSArray arrayWithArray:_arrayChild];          }          else {              return nil;          }      }            - (void)setNodeParent:(XMLNode *)nodeParent      {          _nodeParent = nodeParent;          //计算本结点的深度          if (nil == nodeParent) {              //父结点为nil,当前结点深度为0              _nodeDepth = 0;          }          else {              //当前结点深度为父结点深度+1              _nodeDepth = nodeParent.nodeDepth + 1;          }          //更新子结点的深度          if (_arrayChild.count > 0) {              //遍历子结点              for (XMLNode *nodeChild in _arrayChild) {                  //通过设置父结点的方式更新子结点深度                  nodeChild.nodeParent = self;              }          }      }            - (XMLNode *)nodeParent      {          return _nodeParent;      }            - (NSString *)description      {          if (_strNodeName.length == 0) {              return @"";          }                    NSMutableString *mstrDescription = [NSMutableString string];          //表示深度的空格字符          NSMutableString *mstrSpace = [[NSMutableString alloc] init];          for (int i = 0; i < _nodeDepth; i++) {              [mstrSpace appendString:@" "];          }          [mstrDescription appendString:mstrSpace];          //结点的名称          [mstrDescription appendFormat:@"\r\n%@<%@", mstrSpace, _strNodeName];          //结点的属性          NSArray *arrayKeys = [_dicAttributes allKeys];          for (NSString *strKey in arrayKeys) {              [mstrDescription appendFormat:@" \"%@\"=\"%@\"", strKey, [_dicAttributes objectForKey:strKey]];          }          [mstrDescription appendString:@">"];          //结点的值          if (_strNodeValue.length > 0) {              [mstrDescription appendFormat:@"%@", _strNodeValue];          }          //子结点部分          if (_arrayChild.count > 0) {              //遍历所有子结点              for (XMLNode *nodeChild in _arrayChild) {                  //子结点描述串                  [mstrDescription appendFormat:@"%@", nodeChild];              }              [mstrDescription appendFormat:@"\r\n%@", mstrSpace];          }          //结点的结束          [mstrDescription appendFormat:@"</%@>", _strNodeName];          [mstrSpace release];          //          return mstrDescription;      }            #pragma mark Public            - (void)addChildNode:(XMLNode *)childNode      {          if (nil == _arrayChild) {              _arrayChild = [NSMutableArray arrayWithCapacity:5];          }          //          [_arrayChild addObject:childNode];      }            - (void)clear      {          NSArray *arrayChild = [self children];          //遍历所有子结点          for (XMLNode *node in arrayChild) {              //清空子结点的数据              [node clear];          }          //清空当前结点数据          _nodeDepth = 0;          self.nodeName = nil;          self.nodeValue = nil;          self.nodeAttributesDict = nil;          self.nodeParent = nil;          //清空子结点表          [_arrayChild removeAllObjects];      }            @end                        #pragma mark -      #pragma mark XMLParser interface            @interface XMLParser : NSObject <NSXMLParserDelegate> {      @private                    XMLNode *_rootNode;          XMLNode *_currentNode;      }            - (XMLNode *)parse:(NSData *)dataXML;            @end                        #pragma mark -      #pragma mark XMLParser implementation            @implementation XMLParser            - (XMLNode *)parse:(NSData *)dataXML      {          _rootNode = nil;          _currentNode = nil;          //          NSXMLParser *parser = [[NSXMLParser alloc] initWithData:dataXML]; //设置XML数据          [parser setShouldProcessNamespaces:NO];          [parser setShouldReportNamespacePrefixes:NO];          [parser setShouldResolveExternalEntities:NO];          [parser setDelegate:self];          [parser parse];          [parser release];//20120610增加                    return _rootNode;      }            #pragma mark NSXMLParserDelegate            - (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName         namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict      {          NSLog(@"element start:%@",elementName);          //          if (nil == _rootNode) {              //创建根结点              _rootNode = [[[XMLNode alloc] init] autorelease];              _rootNode.nodeName = elementName;              _rootNode.nodeAttributesDict = attributeDict;              _rootNode.nodeParent = nil;              //              _currentNode = _rootNode;          }          else {              //              XMLNode *nodeChild = [[XMLNode alloc] init];              nodeChild.nodeName = elementName;              nodeChild.nodeAttributesDict = attributeDict;              nodeChild.nodeParent = _currentNode;              //              [_currentNode addChildNode:nodeChild];              _currentNode = nodeChild;              [nodeChild release];          }      }            - (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string      {          //去掉字符串首尾的空字符          NSString *strValidValue = [string stringByTrimmingCharactersInSet:                                     [NSCharacterSet whitespaceAndNewlineCharacterSet]];          NSLog(@"element value:%@",strValidValue);          _currentNode.nodeValue = strValidValue;      }            - (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName         namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName      {          NSLog(@"element end.");          //          _currentNode = _currentNode.nodeParent;      }            - (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError      {          NSLog(@"parse error:%@", parseError);          _rootNode = nil;      }            @end                        #pragma mark -      #pragma mark NSXMLParser Cobbler            @implementation NSXMLParser (Cobbler)            + (XMLNode *)parseToXMLNode:(NSData *)dataXML      {          XMLParser *parser = [[XMLParser alloc] init];          XMLNode *node = [parser parse:dataXML];          [parser release];          return node;      }            @end  


	
				
		
原创粉丝点击