Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS XML writer class

I want to create a game, that will use a level system. So i want to store my levels and to be able to change them during the game (to save the state). So i decided to use XML for storing levels. I found NSXmlParser class for reading from XML, but i can't find a writer to save the level state. In my game the level state and the level are very similar ( i have a lot of movable objects), so i don't wan't to store the level state data separated from the level it belongs. The problem is that i can't find a way to easily modify XML files on iPhone. Maybe i'm using a bad approach.

like image 737
Andrew Avatar asked Oct 19 '10 15:10

Andrew


4 Answers

If you throw the data in an NSDictionary, you could do (with caveats):

[myDictionary writeToFile:pathToPlist atomically:YES];
like image 189
Dave DeLong Avatar answered Oct 28 '22 11:10

Dave DeLong


Try the open source XML stream writer for iOS:

  • Written in Objective-C, a single .h. and .m file
  • One @protocol for namespace support and one for without

Example:

// allocate serializer
XMLWriter* xmlWriter = [[XMLWriter alloc]init];

// start writing XML elements
[xmlWriter writeStartElement:@"Root"];
[xmlWriter writeCharacters:@"Text content for root element"];
[xmlWriter writeEndElement];

// get the resulting XML string
NSString* xml = [xmlWriter toString];

This produces the following XML string:

<Root>Text content for root element</Root>
like image 42
ThomasRS Avatar answered Oct 28 '22 10:10

ThomasRS


I would recommend using KissXML. The author started in a similar situation as you and created an NSXML compatible API wrapper around libxml. He discusses the options and decisions here on his blog.

like image 5
codelark Avatar answered Oct 28 '22 10:10

codelark


You can use the C libary libxml2 to read and write XML. Here's a quick intro: Cocoa Samurai: Getting Some XML Love with libXML2.

However, have you considered using CoreData or implementing the NSCoding protocol? NSCoding would be easier to add to existing classes.

like image 3
Benedict Cohen Avatar answered Oct 28 '22 11:10

Benedict Cohen