开发者

NSDictionary to XML String

开发者 https://www.devze.com 2023-02-27 04:39 出处:网络
Im using XML communication between Iphone and Java server. Using TBXml I parse incoming messages. Whats a good method to convert NSDictionary to xmlStri开发者_Python百科ng?This did the trick for me:

Im using XML communication between Iphone and Java server. Using TBXml I parse incoming messages. Whats a good method to convert NSDictionary to xmlStri开发者_Python百科ng?


This did the trick for me:

NSString *error;
NSData *xmlData = [NSPropertyListSerialization dataFromPropertyList: myDictionary
                                                             format: NSPropertyListXMLFormat_v1_0
                                                   errorDescription: &error];

NSString *xmlString = [[NSString alloc] initWithData: xmlData encoding: NSUTF8StringEncoding];


If your dictionary consists of a limited number of classes, specifically NSData, NSString, NSArray, NSDictionary, NSDate, and NSNumber, you can use NSPropertyListSerialization, in particular the - (BOOL)writeToFile:(NSString *)path atomically:(BOOL)flag method. This will write out the data in plist XML, which is sufficient for simple cases.

Otherwise, you will probably have to write custom serialization methods for custom classes.

0

精彩评论

暂无评论...
验证码 换一张
取 消