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
Swift: Is Correct to Use Stored Properties as Computed Properties
Get The Data from All Children in Firebase Using Swift
Map Object into 2D Array Swift for Tableview Sections
Filemanager and Urlsfordirectory Error in Swift 3 Xcode 8
Swift4: Callback Url Not Approved for This Client Application
Adding an Skscene to a Uiviewcontroller
How Is a Swift Cgvector Created with Dx and Dy (Derivative)
Uicollectionviewcompositionallayout - Center Items in Sections or Groups
Uidocumentinteractioncontroller Does Not Open Other App in iOS 11
How to Dismiss a Modal Segue Then Perform Push Segue to New View Controller
Uibarbuttonitem Changing Title Not Working
Uitabbar Change Background Color of One UItabbaritem on iOS7
How to Access All Images in a .Xcassets at Once
Hls Video Streaming on iOS/Safari
Swift Uiview Opacity Programmatically
Xcode UI Tests Can't Find Views That Are Added Programmatically