I have been reading though heaps of the ios docs from apple, and almost every bit of code to do with xml on there requiers either creating a file with xml data in it or receiving xml over a connection then altering it.
I would like to know if there is a way to just creat some xml.. maybe in a string, then convert it into a NSData object.. I know how to pass a nssting into a nsdata object.. thats not so much of a big deal.. the painfull part at the moment is getting some clear concise examples on how to create my own xml without all the other stuff that apple says you need.. I.e. file or return data.
I currently have no code.. what I will likely do is create a method with some parameters that can receive some information and then pass them in to hopefully create a nice xml string/data object..
if you have any example code, suggestions links to decent tutorials that would be hugely helpfull :)
The easiest way will be using NSPropertyListSerialization. Pass it a dictionary (or any property list object) and it can produce XML (as a string or data) in Apple's property list format.
ReplyDeleteIf you are referring to converting Cocoa objects into XML there are numerous libraries to try. A quick google brought up WonderXML, I haven't used it before but it sounds like this does what you want.
ReplyDeleteIf you don't care about the exact XML format you can always use an NSKeyedArchiver or NSPropertyListSerialization (as jtbandes writes) which can serialize most any Cocoa object to XML. I wouldn't recommend this approach if you intend to read the XML in some other environment or language.
You can use:
ReplyDelete- (BOOL)writeToFile:(NSString *)path atomically:(BOOL)flag
...to write an NSDictionary out to a .plist, which is an XML document with a particular schema. All of the contained objects need to be instances of NSData, NSDate, NSNumber, NSString, NSArray, or NSDictionary.
Use - (id)initWithContentsOfFile:(NSString *)path to read the XML/plist back into a dictionary.
The info file of an iOS project is a property list/plist. They look like this when viewed in a text editor:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleDevelopmentRegion</key>
<string>English</string>
<key>CFBundleDisplayName</key>
<string>App Name</string>
<key>CFBundleExecutable</key>
<string>${EXECUTABLE_NAME}</string>
<key>CFBundleIconFiles</key>
<array>
<string>Icon.png</string>
<string>Icon@2x.png</string>
<string>Icon-iPad.png</string>
<string>Icon-Small-50.png</string>
<string>Icon-Small.png</string>
<string>Icon-Small@2x.png</string>
</array>
</dict>
</plist>