IOS怎样获取属性序列化成xml
1个回答
2015-01-06 · 知道合伙人数码行家
huanglenzhi
知道合伙人数码行家
向TA提问 私信TA
知道合伙人数码行家
采纳数:117538
获赞数:517195
长期从事计算机组装,维护,网络组建及管理。对计算机硬件、操作系统安装、典型网络设备具有详细认知。
向TA提问 私信TA
关注
展开全部
学习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;@endConvertObjToXml.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;@endUSERINFO.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@endCompanyInfo.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;@endCompanyInfo.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>
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询