iphoneXML解析方法

iphone解析xml方式应该有很多种,可以调用xmlib库什么的,那样比较方便,在此只贴出一个比较笨拙的方式,使用代理回调异步解析xml

/**********声明一个代理(只有一个头文件,此函数名称必须存在你调用解析xml类的文件中),在解析类头文件定义对象,当解析完成后回调页面同名函数,更新页面**********/

/********************* *@ 解析用户imsi绑定账号列表的 * 回调函数协议申明 *********************/ #import <UIKit/UIKit.h> @protocol parseBindAccountDelegate //解析message完成后执行的回调函数,成为协议,在页面上要同名 -(void)parseBindAccountCallBack:(NSMutableArray *)myAllAccountDatas; @end 

/***********解析类头文件****************/

/******************* *@ 用户imsi绑定所有账号列表的解析类 * 解析出所有账号,为以用户名切换的情况取id做贮备 ********************/ #import <Foundation/Foundation.h> #import "BindAccountData.h" #import "parseBindAccountDelegate.h" #import "Util.h" @interface ParseBindAccount : NSObject { id<parseBindAccountDelegate> *myparseBindAccountDelegate; //代理协议,声明回调函数 NSMutableArray *myBindAccountDatas; //所有账号信息集合 NSMutableData *connectData; //通用请求url的中转数据; BindAccountData *oneBindAccountData; //一个绑定账号信息 } @property (nonatomic ,retain) id<parseBindAccountDelegate> *myparseBindAccountDelegate; //代理协议,声明回调函数 @property (nonatomic ,retain) NSMutableArray *myBindAccountDatas; //所有账号信息集合 @property (nonatomic ,retain) NSMutableData *connectData; //通用请求url的中转数据; @property (nonatomic ,retain) BindAccountData *oneBindAccountData; //一个绑定账号信息 /* 调用解析方法 解析XML */ -(void)ParseBindAccount:(NSMutableData *)inData; @end

/***********解析文件***************/

/******************* *@ 用户imsi绑定所有账号列表的解析类 * 解析出所有账号,为以用户名切换的情况取id做贮备 ********************/ #import "ParseBindAccount.h" @implementation ParseBindAccount @synthesize myparseBindAccountDelegate; //代理协议,声明回调函数 @synthesize myBindAccountDatas; //所有账号信息集合 @synthesize connectData; //通用请求url的中转数据; @synthesize oneBindAccountData; //一个绑定账号信息 /* 调用解析方法 解析XML */ -(void)ParseBindAccount:(NSMutableData *)inData{ [Util showLog:@"ParseBindAccount"]; //to init data myBindAccountDatas=[[NSMutableArray alloc]init];//all sys msg in it oneBindAccountData=[[BindAccountData alloc]init]; connectData = [[NSMutableData alloc] init];//xml数据分配空间 connectData = inData;//recive data of xml NSXMLParser *paser = [[NSXMLParser alloc] initWithData:connectData]; [paser setDelegate:self]; [paser parse]; //执行这句 开始回调解析函数-》parserDidStartDocument -》didStartElement [paser release]; [connectData release]; } /* 开始解析xml文件 */ - (void)parserDidStartDocument:(NSXMLParser *)parser { [Util showLog:@"parserDidStartDocument"]; } /* 开始对一个xml元素开始解析 将Element进行初始化 */ -(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict{ [Util showLog:@"didStartElement"]; [oneBindAccountData release]; oneBindAccountData=[[BindAccountData alloc]init]; if ([elementName isEqualToString:@"list"]) { [Util showLog:@"enter list"]; NSString *action=[attributeDict objectForKey:@"action"]; oneBindAccountData.action=action; } else if ([elementName isEqualToString:@"item"]){ //出现item可能为失败,但有的操作也会要成功信息,再次判断action,看是取error还是msg if ([oneBindAccountData.action compare:@"true"]==0) { oneBindAccountData.userid=[attributeDict objectForKey:@"userid"]; oneBindAccountData.loginname=[attributeDict objectForKey:@"loginname"]; oneBindAccountData.mainflag=[attributeDict objectForKey:@"mainflag"]; }else {//error oneBindAccountData.error=[attributeDict objectForKey:@"error"]; } [myBindAccountDatas addObject:oneBindAccountData]; } } /* 将得到的字符添加到Element后面去 */ -(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{ [Util showLog:@"foundCharacters"]; } /* 对XML元素解析完成 将Element添加到数组Elements中去 */ -(void)parser:(NSXMLParser *)paser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{ [Util showLog:@"didEndElement"]; } /* XML解析完毕 将相关信息显示到界面中 */ - (void)parserDidEndDocument:(NSXMLParser *)parser{ [Util showLog:@"parserDidEndDocument"]; if (self.myparseBindAccountDelegate != nil) { //完成线程 调用回调函数 [self.myparseBindAccountDelegate parseBindAccountCallBack:myBindAccountDatas]; } [myparseBindAccountDelegate release]; } @end

/*************xml文件****************/

<?xml version="1.0" encoding="UTF-8"?> <list action="true"><item userid="11111111111111" loginname="safasdfas" mainflag="0"/><item userid="2222222222222222222" loginname="sadfasd" mainflag="0"/><item userid="3333333333333333" loginname="adfsd" mainflag="0"/><item userid="455555555555" loginname="dsafdsaf" mainflag="0"/><item userid="44444444444444" loginname=dsfdsf" mainflag="1"/></list>" 

 

你可能感兴趣的:(xml,list,action,interface,encoding,attributes)