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

Why is this Javascript much *slower* than its jQuery equivalent?

I have a HTML list of about 500 items and a "filter" box above it. I started by using jQuery to filter the list when I typed a letter (timing code added later): $('#filter').keyup( function() { var jqStart = (new Date).getTime(); var search = $(this).val().toLowerCase(); var $list = $('ul.ablist > li'); $list.each( function() { if ( $(this).text().toLowerCase().indexOf(search) === -1 ) $(this).hide(); else $(this).show(); } ); console.log('Time: ' + ((new Date).getTime() - jqStart)); } ); However, there was a couple of seconds delay after typing each letter (particularly the first letter). So I thought it may be slightly quicker if I used plain Javascript (I read recently that jQuery's each function is particularly slow). Here's my JS equivalent: document.getElementById('filter').addEventListener( 'keyup', function () { var jsStart = (new Date).getTime()...

Is it possible to have IF statement in an Echo statement in PHP

Thanks in advance. I did look at the other questions/answers that were similar and didn't find exactly what I was looking for. I'm trying to do this, am I on the right path? echo " <div id='tabs-".$match."'> <textarea id='".$match."' name='".$match."'>". if ($COLUMN_NAME === $match) { echo $FIELD_WITH_COLUMN_NAME; } else { } ."</textarea> <script type='text/javascript'> CKEDITOR.replace( '".$match."' ); </script> </div>"; I am getting the following error message in the browser: Parse error: syntax error, unexpected T_IF Please let me know if this is the right way to go about nesting an IF statement inside an echo. Thank you.