IOS 反射获取属性,序列化成xml
学习Object-C不足一个月。 写的较简单。支持复杂对象,目前对象中只支持NSSTRING类型和自定义类。
因为项目需要,调用service时候,一定要用xml作为数据,我Google了两天,没找到支持复杂对象的序列化。不知各位大牛有什么好的类库实现了对象和xml的互转?以前习惯在.net下有现成的,转到Object-C下面,貌似好多要自己写!各种纠结!
下面是我用Object-C实现的将对象转为xml的方法。但是还没研究出来如何将xml转为对象的解决方案。忘高手指导一二。
ConvertObjToXml.h
// // ConvertObjToXml.h //// // Created by Elliott on 13-3-5. // Copyright (c) 2013年 Elliott. All rights reserved. // #import <Foundation/Foundation.h> @interface ConvertObjToXml : NSObject +(NSString *)Convert:(id)obj; @end
ConvertObjToXml.m
// // ConvertObjToXml.m // WebServiceTest // // Created by Elliott on 13-3-5. // Copyright (c) 2013年 Elliott. All rights reserved. // #import <objc/runtime.h> #import "ConvertObjToXml.h" @implementation ConvertObjToXml +(NSString *)Convert:(id)obj { unsigned int outCount, i; objc_property_t *properties = class_copyPropertyList([obj class], &outCount); NSString *xml=[[NSString alloc]initWithFormat:@"<%@>",[obj class]]; for(i = 0; i < outCount; i++) { objc_property_t property = properties[i]; NSString *key=[[NSString alloc]initWithCString:property_getName(property) encoding:NSUTF8StringEncoding]; id value=[obj valueForKey:key]; if (value!=nil) { //IF NOT NSSTRING,LOOP!!!!!! if (![value isKindOfClass:[NSString class]]) { //NSLog(@"子类:%@",[ConvertObjToXml Convert:[obj valueForKey:key]]); xml=[xml stringByAppendingFormat:@"%@",[ConvertObjToXml Convert:[obj valueForKey:key]]]; } else { xml=[xml stringByAppendingFormat:@"<%@>%@</%@>",key,value,key]; } } else { xml=[xml stringByAppendingFormat:@"<%@>%@</%@>",key,@"",key]; } } xml=[xml stringByAppendingFormat:@"</%@>",[obj class]]; //NSLog(@"结果:%@",xml); free(properties); return xml; } @end
调用方式:
NSString *xml= [ConvertObjToXml Convert:obj]; NSLog(@"Result:%@",xml);
案例:
首先定义了两个类,一个UserInfo,一个companyinfo。UserInfo中包含companyinfo这个类。
USERINFO.h
// // USERINFO.h // // // Created by Elliott on 13-3-5. // Copyright (c) 2013年 Elliott. All rights reserved. // #import <Foundation/Foundation.h> @class CompanyInfo; @interface USERINFO : NSObject @property(nonatomic,strong) CompanyInfo *CompanyInfo; @property(nonatomic,strong) NSString *USERID; @property(nonatomic,strong) NSString *PASSWORD; @property(nonatomic,strong) NSString *USERTYPE; @end
USERINFO.m
// // USERINFO.m // // // Created by Elliott on 13-3-5. // Copyright (c) 2013年 Elliott. All rights reserved. // #import <objc/runtime.h> #import "USERINFO.h" #import "ConvertObjToXml.h" @implementation USERINFO @end
CompanyInfo.h
// // CompanyInfo.h // // // Created by Elliott on 13-3-5. // Copyright (c) 2013年 Elliott. All rights reserved. // #import <Foundation/Foundation.h> @interface CompanyInfo : NSObject @property(nonatomic,strong) NSString *CompanyName; @property(nonatomic,strong) NSString *CompanyCode; @end
CompanyInfo.m
// // CompanyInfo.m // // // Created by Elliott on 13-3-5. // Copyright (c) 2013年 Elliott. All rights reserved. // #import "CompanyInfo.h" @implementation CompanyInfo @end
调用:
USERINFO *user=[[USERINFO alloc] init]; user.USERID=@"aaa"; CompanyInfo *info=[[CompanyInfo alloc]init]; info.CompanyName=@"companyname"; info.CompanyCode=@"code"; user.CompanyInfo=info; NSLog(@"%@",[ConvertObjToXml Convert:user]);
结果:
<USERINFO><CompanyInfo><CompanyName>companyname</CompanyName><CompanyCode>code</CompanyCode></CompanyInfo><USERID>aaa</USERID><PASSWORD></PASSWORD><USERTYPE></USERTYPE></USERINFO>