文件名称:Objective-c对象组装XML
文件大小:50KB
文件格式:ZIP
更新时间:2014-11-01 10:24:09
iPhone Objective-c XML 组装 对象
【原创】转帖请注明来源,谢谢 -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
【文件预览】:
Xml Package
----Xml Package()
--------XmlResolve2.m(3KB)
--------Xml_PackageViewController.h(359B)
--------main.m(357B)
--------template2.xml(562B)
--------XmlResolve2.h(672B)
--------template1.xml(213B)
--------XmlPackage.h(792B)
--------Xml Package-Info.plist(1KB)
--------Xml_PackageAppDelegate.m(2KB)
--------Xml_PackageAppDelegate.h(433B)
--------XmlNode.h(657B)
--------XmlNode.m(2KB)
--------en.lproj()
--------Xml Package-Prefix.pch(328B)
--------Xml_PackageViewController.m(3KB)
--------XmlPackage.m(3KB)
----Xml Package.xcodeproj()
--------project.xcworkspace()
--------project.pbxproj(15KB)
--------xcuserdata()