ios中GDataXML解析XML文档
2013-08-22 11:52 甘超波 阅读(3833) 评论(0) 编辑 收藏 举报参考文章 http://blog.csdn.net/ryantang03/article/details/7868246
适合解析一个节点多个属性要用GDataXml 格式如下
<?xml version="1.0" encoding="UTF-8"?>
<result spend_time="16ms">>
<data>
<item wait_count="4"max_wait_time="10"branch_id="0">
<item wait_count="0"max_wait_time="7" branch_id="232051000" />
<item wait_count="0"max_wait_time="4" branch_id="232051001" />
</item>
</data>
</result>
NSString *path=[[NSBundle mainBundle] pathForResource:@"1" ofType:@"xml"]; NSData *data=[[NSData alloc] initWithContentsOfFile:path]; GDataXMLDocument *doc=[[GDataXMLDocument alloc] initWithData:data options:0 error:nil]; //获取根节点 GDataXMLElement *rootElement=doc.rootElement; NSArray *arr=[rootElement nodesForXPath:@"//item/item" error:nil]; for (GDataXMLElement *ele in arr) { NSLog(@"-->%@--->%@",[[ele attributeForName:@"wait_count"]stringValue],[[ele attributeForName:@"branch_name"]stringValue]); }
一个节点一个属性,推荐使用下面方法
<?xml version="1.0" encoding="utf-8"?>
<Users>
<User id="001">
<name>Ryan</name>
<age>24</age>
</User>
<User id="002">
<name>Tang</name>
<age>23</age>
</User>
</Users>
封装方法
#import <Foundation/Foundation.h> @protocol UseXmlParser <NSObject> @optional - (void)NetWorkBackConnectID:(int)connectID BackDic:(NSMutableDictionary*)aBackDic WithNetState:(int)netState; @end @interface UseXmlParser : NSObject<NSXMLParserDelegate> { id aTarget; int aConnectId; NSMutableData *parData; NSXMLParser *xmlParser; NSMutableArray *dicNameArray; BOOL endStringElement; NSMutableString *keyString; NSMutableDictionary *xmlDataSource; } -(id)initWithParserData:(NSData*)pData target:(id)toTarget connectId:(int)bConnectId; -(void)parser; @end ========== #import "UseXmlParser.h" @implementation UseXmlParser -(id)initWithParserData:(NSData*)pData target:(id)toTarget connectId:(int)bConnectId{ if (self = [super init]) { aTarget = toTarget; aConnectId = bConnectId; dicNameArray = [[NSMutableArray alloc] initWithCapacity:0]; keyString = [[NSMutableString alloc] initWithCapacity:0]; xmlDataSource=[[NSMutableDictionary alloc] initWithCapacity:0]; endStringElement = FALSE; parData = [[NSMutableData alloc] initWithData:pData]; } return self; } -(void)parser{ xmlParser = [[NSXMLParser alloc] initWithData:parData]; [xmlParser setDelegate:self]; [xmlParser parse];//开始解析 [xmlParser release]; } -(NSMutableDictionary*) getDic { NSMutableDictionary *dic=xmlDataSource; for (int i=0; i<[dicNameArray count]; i++) { dic=[dic objectForKey:(NSString*)[dicNameArray objectAtIndex:i]]; } return dic; } - (NSString*)getListElement:(NSString*)elementName From:(NSArray*)array { NSString *temp = elementName; for (int i=0; i<100000; i++) { if ([array containsObject:elementName]) { elementName = [NSString stringWithFormat:@"%@%d", temp, i+1]; } else { return elementName; } } return nil; } - (id)getStringByKey:(NSString*)key { NSArray *array = [key componentsSeparatedByString:@"/"]; NSDictionary *dic = [NSDictionary dictionary]; for (int i=0; i<[array count]-1; i++) { dic = [dic objectForKey:[array objectAtIndex:i]]; } NSString *temp = [dic objectForKey:[array objectAtIndex:[array count]-1]]; if (!temp) { temp = @""; } return temp; } -(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{ if ([dicNameArray count]>0) { NSMutableDictionary *dic = [self getDic]; elementName = [self getListElement:elementName From:[dic allKeys]]; [dic setObject:[[NSMutableDictionary alloc] init] forKey:elementName]; [dicNameArray addObject:elementName]; } else { [xmlDataSource setObject:[[NSMutableDictionary alloc] init] forKey:elementName]; [dicNameArray addObject:elementName]; } endStringElement = TRUE; [keyString setString:@""]; } -(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{ [keyString appendString:string]; } - (NSMutableDictionary*)getLastDic { NSMutableDictionary *dic=xmlDataSource; for (int i=0; i<[dicNameArray count]-1; i++) { dic=[dic objectForKey:(NSString*)[dicNameArray objectAtIndex:i]]; } return dic; } -(void) removeLastDic { if ([dicNameArray count]>0) { [dicNameArray removeLastObject]; } } -(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{ if (endStringElement) { if (![keyString isEqualToString:@"\n"]) { if ([keyString hasPrefix:@"\n"]) { [keyString setString:[keyString substringFromIndex:1]]; } NSMutableDictionary *dic = [self getLastDic]; [dic setObject:[NSString stringWithString:keyString] forKey:[dicNameArray objectAtIndex:[dicNameArray count]-1]]; } } [keyString setString:@""]; endStringElement = FALSE; [self removeLastDic]; } - (void)parserDidEndDocument:(NSXMLParser *)parser { NSLog(@" \nxml解析返回-->> %@",xmlDataSource); if (aTarget && [aTarget respondsToSelector:@selector(NetWorkBackConnectID:BackDic:WithNetState:)]) { [aTarget NetWorkBackConnectID:aConnectId BackDic:xmlDataSource WithNetState:0]; } } -(void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError{ NSLog(@"--error-->%@",parseError.localizedDescription); } -(void)dealloc{ [dicNameArray release]; [keyString release]; [xmlDataSource release]; [super dealloc]; } @end
使用方式
- (IBAction)click:(id)sender { UseXmlParser *parser1=[[UseXmlParser alloc] initWithParserData:data target:self connectId:100]; [parser1 parser]; [parser1 release]; } - (void)NetWorkBackConnectID:(int)connectID BackDic:(NSMutableDictionary*)aBackDic WithNetState:(int)netState{ NSLog(@"--%@",aBackDic); }
目前我正在专注NLP,请立刻加微信/QQ号 546611623, 免费送你原创《NLP高级执行师》高清视频