iphone – NSDictionary到XML

iphone – NSDictionary到XML,第1张

概述我正在尝试将NSDictionary转换为 XML. (我成功地将NSDictionary转换为 JSON).但现在我需要将NSDictionary转换为XML.在Objective-C中是否有类似JSON的内置序列化程序? int r = arc4random() % 999999999;//simulate my NSDictionary (to be turned into xml)N 我正在尝试将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"];

… //省略了其他不相关的代码

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

NSLog不会以XML格式打印字符串.它像NSDictionary一样打印出来.
我应该使用什么来将我的NSDictionary序列化为XML?

解决方法 propertyListFromData:…返回“属性列表对象”,即根据数据的内容,数组或字典.你真正感兴趣的东西(xml)由dataFromPropertyList:…返回,因此存储在你的theBodyData变量中.

试试这个:

NSLog(@"XML: %@",[[[Nsstring alloc] initWithData:theBodyData enCoding:NSUTF8StringEnCoding] autorelease]);
总结

以上是内存溢出为你收集整理的iphone – NSDictionary到XML全部内容,希望文章能够帮你解决iphone – NSDictionary到XML所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1045652.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-24
下一篇 2022-05-24

发表评论

登录后才能评论

评论列表(0条)

保存