Converting Nsdictionary to Xml

Converting NSDictionary to XML

The short answer is: No, there is no built-in ability to do this in the Cocoa libraries.

Because you're writing rather than parsing, and presumably dealing with a limited universe of possible tags, the code to output XML is actually not that complicated. It should just be a simple method in your Invoice object, something like:

- (NSString*) postStringInXMLFormat
{
NSMutableString* returnValue = [[NSMutableString alloc] init];
if([self email])
{
[returnValue appendString:@"<email>"];
[returnValue appendString:[self email]];
[returnValue appendString:@"</email>"];
}
if([self invoice_date])
...

and so on. At the end return

[NSString stringWithString:returnValue]

There are plenty of third-party projects out there that try to generalize this process; several of them are listed in this answer:

Xml serialization library for iPhone Apps

But if all you're looking to do is create a single, stable format that your server side will recognize, and you don't have a ridiculous number of entities to convert, it's probably less work to roll your own.

NSDictionary to XML

propertyListFromData:... returns a "property list object", that is, depending on the contents of the data, an array or a dictionary. The thing that you're actually interested in (the xml) is returned by dataFromPropertyList:... and thus stored in your theBodyData variable.

Try this:

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

Serializing an NSDictionary to XML File

at that point it will be simpler to do a custom serialization...

for (id key in dictionary)
{
[someMutableString appendFormat:@"%@ = %@\n",key,[dictionary objectForKey:key]]];
}

converting nsdictionary to xml format in iphone

The NSPropertyListSerialization class should be able to do this for you.

How to convert .plist xml string to NSDictionary

You can use PropertyListSerialization from the Foundation framework to create a NSDictionary
from the given property list data. Swift 3 Example:

do {
if let plist = try PropertyListSerialization.propertyList(from: plistData, format: nil)
as? NSDictionary {
// Successfully read property list.
print(plist)

} else {
print("not a dictionary")
}
} catch let error {
print("not a plist:", error.localizedDescription)
}

And with

    if let plist = try PropertyListSerialization.propertyList(from: plistData, format: nil)
as? [String: Any] { ... }

you'll get the result as a Swift Dictionary.



Related Topics



Leave a reply



Submit