Reputation: 5510
I have been reading through heaps of the ios docs from Apple, and almost every bit of code to do with xml on there requires 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 create some xml.. maybe in a string, then convert it into a NSData object.. I know how to pass a NSString into a NSData object.. thats not so much of a big deal.. the painful 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.
Upvotes: 0
Views: 481
Reputation: 10296
You can use:
- (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>[email protected]</string>
<string>Icon-iPad.png</string>
<string>Icon-Small-50.png</string>
<string>Icon-Small.png</string>
<string>[email protected]</string>
</array>
</dict>
</plist>
Upvotes: 0
Reputation: 1532
If 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.
If 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.
Upvotes: 1
Reputation: 118651
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.
Upvotes: 0