Skip to main content

how to write xml with objectiv-c and place it into an NSData object



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 :)


Comments

  1. 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.

    ReplyDelete
  2. 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.

    ReplyDelete
  3. 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>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>

    ReplyDelete

Post a Comment

Popular posts from this blog

Slow Android emulator

I have a 2.67 GHz Celeron processor, 1.21 GB of RAM on a x86 Windows XP Professional machine. My understanding is that the Android emulator should start fairly quickly on such a machine, but for me it does not. I have followed all instructions in setting up the IDE, SDKs, JDKs and such and have had some success in staring the emulator quickly but is very particulary. How can I, if possible, fix this problem?

Java Urban Myths

Along the line of C++ Urban Myths and Perl Myths : What are the Java Urban Myths? That is, the ideas and conceptions about Java that are common but have no actual roots in reality . As a Java programmer, what ideas held by your fellow Java programmers have you had to disprove so often that you've come to believe they all learned at the feet of the same drunk old story-teller? Ideally, you would express these myths in a single sentence, and include an explanation of why they are false.

CCNA 1 Final Exam 2011 latest (hot hot hot)

  Hi! I have been posted content of ccna1 final exam (latest and only question.) I will post the answer and insert image on sunday. If you care, please subscribe your email an become a first person have full test content. Subcribe now  Some question  have not content because this question have images content. So that can you wait for me? SUNDAY 1. A user sees the command prompt: Router(config-if)# . What task can be performed at this mode? Reload the device. Perform basic tests. Configure individual interfaces. Configure individual terminal lines. 2. Refer to the exhibit. Host A attempts to establish a TCP/IP session with host C. During this attempt, a frame was captured with the source MAC address 0050.7320.D632 and the destination MAC address 0030.8517.44C4. The packet inside the captured frame has an IP source address 192.168.7.5, and the destination IP address is 192.168.219.24. At which point in the network was this packet captured? leaving host A leaving ATL leaving...