dclaze
dclaze

Reputation: 61

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 xmlString?

Upvotes: 1

Views: 2316

Answers (2)

mpemburn
mpemburn

Reputation: 2884

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

Upvotes: 3

Don
Don

Reputation: 3684

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.

Upvotes: 0

Related Questions