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

[韓日関係] 首相含む大幅な内閣改造の可能性…早ければ来月10日ごろ=韓国

div not scrolling properly with slimScroll plugin

I am using the slimScroll plugin for jQuery by Piotr Rochala Which is a great plugin for nice scrollbars on most browsers but I am stuck because I am using it for a chat box and whenever the user appends new text to the boxit does scroll using the .scrollTop() method however the plugin's scrollbar doesnt scroll with it and when the user wants to look though the chat history it will start scrolling from near the top. I have made a quick demo of my situation http://jsfiddle.net/DY9CT/2/ Does anyone know how to solve this problem?

Why does this javascript based printing cause Safari to refresh the page?

The page I am working on has a javascript function executed to print parts of the page. For some reason, printing in Safari, causes the window to somehow update. I say somehow, because it does not really refresh as in reload the page, but rather it starts the "rendering" of the page from start, i.e. scroll to top, flash animations start from 0, and so forth. The effect is reproduced by this fiddle: http://jsfiddle.net/fYmnB/ Clicking the print button and finishing or cancelling a print in Safari causes the screen to "go white" for a sec, which in my real website manifests itself as something "like" a reload. While running print button with, let's say, Firefox, just opens and closes the print dialogue without affecting the fiddle page in any way. Is there something with my way of calling the browsers print method that causes this, or how can it be explained - and preferably, avoided? P.S.: On my real site the same occurs with Chrome. In the ex