简体   繁体   English

NSDictionary到XML

[英]NSDictionary to XML

I'm trying to convert a NSDictionary to XML. 我正在尝试将NSDictionary转换为XML。 (I was successful in transforming NSDictionary to JSON). (我成功地将NSDictionary转换为JSON)。 But now I need to transform NSDictionary to XML. 但现在我需要将NSDictionary转换为XML。 Is there a built-in serializer in Objective-C like the one for JSON? 在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"];

...//other irrelevant code omitted ... //省略了其他不相关的代码

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]);

The NSLog doesn't print a string in XML format. NSLog不会以XML格式打印字符串。 It prints it like a NSDictionary. 它像NSDictionary一样打印出来。 What should I use to serialize my NSDictionary to XML? 我应该使用什么来将我的NSDictionary序列化为XML?

propertyListFromData:... returns a "property list object", that is, depending on the contents of the data, an array or a dictionary. propertyListFromData:...返回“属性列表对象”,即根据数据的内容,数组或字典。 The thing that you're actually interested in (the xml) is returned by dataFromPropertyList:... and thus stored in your theBodyData variable. 你真正感兴趣的东西(xml)由dataFromPropertyList:...返回,因此存储在你的theBodyData变量中。

Try this: 尝试这个:

NSLog(@"XML: %@", [[[NSString alloc] initWithData:theBodyData encoding:NSUTF8StringEncoding] autorelease]);

There are many different varieties of XML. 有许多不同种类的XML。 If you're not picky about the specific tags, and if the contents of your dictionary is limited to types used in property lists (NSString, NSNumber, NSDate, etc.) you can write your dictionary to a property list in one line: 如果您对特定标记没有挑剔,并且字典的内容仅限于属性列表中使用的类型(NSString,NSNumber,NSDate等),您可以将字典写入一行中的属性列表:

[myDict writeToFile:somePath atomically:YES];

If you'd prefer to keep the XML in memory instead of writing to a file, use NSPropertyListSerialization as you're doing. 如果您希望将XML保留在内存中而不是写入文件,请使用NSPropertyListSerialization。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM