ace*_*des 5 xml iphone xcode objective-c ipad
我正在尝试将NSDictionary转换为XML.(我成功地将NSDictionary转换为JSON).但现在我需要将NSDictionary转换为XML.在Objective-C中是否有类似JSON的内置序列化程序?
int r = arc4random() % 999999999;
//simulate my NSDictionary (to be turned into xml)
NSString *name = [NSString stringWithFormat:@"Posted using iPhone_%d", r];
NSString *stock_no = [NSString stringWithFormat:@"2342_%d", r];
NSString *retail_price = @"12345";
NSArray *keys = [NSArray arrayWithObjects:@"name", @"stock_no", @"retail_price", nil];
NSArray *objects = [NSArray arrayWithObjects:name,stock_no,retail_price, nil];
NSDictionary *theRequestDictionary = [NSDictionary dictionaryWithObjects:objects forKeys:keys];
NSDictionary *theFinalRequestDictionary = [NSDictionary dictionaryWithObject:theRequestDictionary forKey:@"product"];
Run Code Online (Sandbox Code Playgroud)
... //省略了其他不相关的代码
NSData *theBodyData = [NSPropertyListSerialization dataFromPropertyList:theFinalRequestDictionary format:NSPropertyListXMLFormat_v1_0 errorDescription:nil];
NSPropertyListFormat format;
id XMLed = [NSPropertyListSerialization propertyListFromData:theBodyData
mutabilityOption:NSPropertyListImmutable
format:&format
errorDescription:nil];
NSLog(@"the XMLed is this: %@", [NSString stringWithFormat:@"%@", XMLed]);
Run Code Online (Sandbox Code Playgroud)
NSLog不会以XML格式打印字符串.它像NSDictionary一样打印出来.我应该使用什么来将我的NSDictionary序列化为XML?
propertyListFromData:...返回一个“属性列表对象”,即根据数据内容,是数组还是字典。您真正感兴趣的内容(xml)由您的变量返回dataFromPropertyList:...并存储在您的theBodyData变量中。
尝试这个:
NSLog(@"XML: %@", [[[NSString alloc] initWithData:theBodyData encoding:NSUTF8StringEncoding] autorelease]);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4608 次 |
| 最近记录: |