NSDictionary to XML String
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.
精彩评论