开发工具:
文件大小: 50kb
下载次数: 0
上传时间: 2011-10-12
详细说明: 【原创】转帖请注明来源,谢谢 -tag 1- 测试无内存泄露 1 创建数据对象() NSMutableDictionary *map = [[NSMutableDictionary alloc]init]; [map setObject:@"a" forKey:@"author"]; [map setObject:@"b" forKey:@"title"]; [map setObject:@"c" forKey:@"content"]; 或者 NSMutableArray *list = [[NSMutableArray alloc]init]; NSMutableDictionary *map1 = [[NSMutableDictionary alloc]init]; [map1 setObject:@"a1" forKey:@"author"]; [map1 setObject:@"b1" forKey:@"title"]; [map1 setObject:@"c1" forKey:@"content"]; [list addObject:map1]; NSMutableDictionary *map2 = [[NSMutableDictionary alloc]init]; [map2 setObject:@"a2" forKey:@"author"]; [map2 setObject:@"b2" forKey:@"title"]; [map2 setObject:@"c2" forKey:@"content"]; [list addObject:map2]; 2 调用封装,声明对象名称及XML模板,组装器会根据XML模板填充数据。 XmlPackage *xmlPackage = [[XmlPackage alloc]init]; NSData *data = [xmlPackage objctPackage:map objectName:@"book1" xmlTemplateName:@"template1"]; 或者 NSData *data = [xmlPackage listPackage:list objectName:@"book1" xmlTemplateName:@"template1"]; 3 返回DATA 4 利用解析类解析并展现到UIView 部分代码如下: @implementation XmlPackage @synthesize obj; @synthesize isList; @synthesize xmlString; @synthesize objectName; @synthesize lvUp; @synthesize root; @synthesize buildEnd; -(NSData *)objctPackage:(NSMutableDictionary *)object objectName:(NSString *)name xmlTemplateName:(NSString *)templateName { NSMutableArray *array = [[NSMutableArray alloc]init]; [array addObject:object]; NSData *data = [self listPackage:array objectName:name xmlTemplateName:templateName]; [array release]; return data; } -(NSData *)listPackage:(NSMutableArray *)objects objectName:(NSString *)name xmlTemplateName:(NSString *)templateName { isList = YES; self.objectName = name; NSString *path = [[NSBundle mainBundle] pathForResource:templateName ofType:@"xml"]; NSFileHandle *file = [NSFileHandle fileHandleForReadingAtPath:path]; NSData *xmlData = [file readDataToEndOfFile]; NSXMLParser *xmlRead = [[NSXMLParser alloc] initWithData:xmlData]; [xmlRead setDelegate:self]; [xmlRead parse]; [xmlRead release]; //get dataTemplate for (int i=0;i<[objects count]; i++) { NSMutableDictionary *map = [objects objectAtIndex:i]; XmlNode *node = [[[XmlNode alloc]init]autorelease]; node.nodeName = objectName; [node addChildByMap:map]; [lvUp addChildNode:node]; } //get root's xmlString NSMutableString *x = [self.root getXmlString]; //NSLog(@"%@",x); if ((int)[x length]>0) { return [x dataUsingEncoding:NSUTF8StringEncoding]; } return nil; } - (void)dealloc { [obj release]; [xmlString release]; [objectName release]; [lvUp release]; //[root release]; [super dealloc]; } - (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string { } - (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName { } - (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict { if ([elementName isEqualToString:objectName]) { buildEnd = YES; }else{ if (!buildEnd) { //create node XmlNode *node = [[XmlNode alloc]init]; node.nodeName = elementName; //dataTemplate building... if (lvUp!=nil) { [lvUp addChildNode:node]; }else{ root = node; } lvUp = node; } } } @end ===== #import "XmlNode.h" @implementation XmlNode @synthesize nodeName; @synthesize nodeValue; @synthesize isLast; @synthesize list; @synthesize xmlString; - (void)dealloc { [nodeName release]; [nodeValue release]; [list release]; [xmlString release]; [super dealloc]; } -(NSString *)getXmlString { xmlString = [[NSMutableString alloc]init]; [xmlString appendString:@""]; [self getNodeStr:self string:xmlString]; return xmlString; } -(void)getNodeStr:(XmlNode *)node string:(NSMutableString *)str { if (node.isLast) { NSString *res = [[NSString alloc]initWithFormat:@"<%@>%@%@>", node.nodeName, node.nodeValue, node.nodeName]; [str appendString:res]; [res release]; }else{ NSMutableString *a = [[[NSMutableString alloc]init]autorelease]; NSMutableArray *nodelist = [node getChilds]; for (int i=0;i<[nodelist count];i++) { XmlNode *node = [nodelist objectAtIndex:i]; [node getNodeStr:node string:a]; } NSString *res = [[NSString alloc]initWithFormat:@"<%@>%@%@>", node.nodeName, a, node.nodeName]; [str appendString:res]; [res release]; } } -(void)addChildNode:(XmlNode *)node; { if (list==nil) { list = [[NSMutableArray alloc]init]; } [list addObject:node]; isLast = NO; } -(NSMutableArray *)getChilds { return list; } -(void)addChildByMap:(NSMutableDictionary *)map { NSEnumerator *keys = [map keyEnumerator]; id key; while (key = [keys nextObject]) { XmlNode *node = [[[XmlNode alloc]init]autorelease]; node.isLast = YES; node.nodeName = key; node.nodeValue = [map objectForKey:key]; [self addChildNode:node]; } } @end ...展开收缩
(系统自动生成,下载前可以参看下载内容)
下载文件列表
相关说明
- 本站资源为会员上传分享交流与学习,如有侵犯您的权益,请联系我们删除.
- 本站是交换下载平台,提供交流渠道,下载内容来自于网络,除下载问题外,其它问题请自行百度。
- 本站已设置防盗链,请勿用迅雷、QQ旋风等多线程下载软件下载资源,下载后用WinRAR最新版进行解压.
- 如果您发现内容无法下载,请稍后再次尝试;或者到消费记录里找到下载记录反馈给我们.
- 下载后发现下载的内容跟说明不相乎,请到消费记录里找到下载记录反馈给我们,经确认后退回积分.
- 如下载前有疑问,可以通过点击"提供者"的名字,查看对方的联系方式,联系对方咨询.