views:

274

answers:

3

Hi folks,

I have an iPhone App that uses a relatively small amount of data. I'd like to save the data in the XML format and be able to load this in memory to Objective-C objects. I'd like to use iPhone SDK facilities like NSPropertyListSerialization or writeToFile:atomically: classes and methods.

The NSPropertyListSerialization documentation says that

The NSPropertyListSerialization class provides methods that convert property list objects to and from several serialized formats. Property list objects include NSData, NSString, NSArray, NSDictionary, NSDate, and NSNumber objects.

Suppose I'd like to save a couple of Person objects (a family) whose attributes are all property-list types. The only-way I managed to save this in XML Format is by doing:

// container for person objects
NSMutableArray *family = [[NSMutableArray alloc] init];

for (int i = 0; i < numberOfPeople; i++) {      
 // simulate person's attributes
 NSArray *keys = [[NSArray alloc] initWithObjects:
       @"id", 
       @"name",
       @"age",
       Nil];

 NSArray *values = [[NSArray alloc] initWithObjects:
         [NSNumber numberWithInt:i],
         @"Edward", 
         [NSNumber numberWithInt:10],
         Nil];

 // create a Person (a dictionary)
 NSDictionary *person = [[NSDictionary alloc] initWithObjects:values forKeys:keys];
 [family addObject:person];
 [person release];  
}

// save the "person" object to the property list
[family writeToFile:@"<path>/family.plist" atomically:YES];
[family release];

which would generate the family.plist file.

Note, however, that my Person object is actually a NSDictionary object (which I don't think subclassing would be a good idea) with attributes as keys. Is there any way to create a Objective-C class like this:

@interface Person : NSObject {
 NSString *_id; 
 NSString *_name;
 NSNumber *_age;
}
@end

and serialize it to a plist/XML file? (I need to save the object in text format (not binary) so that I can edit such data in a text editor and allow my application to load this at runtime, converting to Objective-C objects normally).

Thanks in advance, Eduardo

+2  A: 

If you haven't already read it, I would highly recommend the Archives and Serializations Programming Guide, particularly the parts about encoding and decoding objects using the NSCoding protocol.

If you use that protocol, your objects will be stored as NSData. But you could imitate it (i.e. create your own protocol) so that your classes return dictionary representations of themselves, and can initialize themselves from a dictionary representation (I don't think there's a way to automatically do that). However, the result would essentially be the same as the family.plist file you posted.

If you're looking for a way to get an XML file like this:

<person>
    <id>0</id>
    <name>Edward</name>
    <age>10</age>
</person>

then plist isn't what you want.

Brian
Brian. I have read about Archives and Serializations. The main point is that, as you said, the object will be saved in the .plist file as <data> (NSData). I could make utility methods to represent/initialize the person class with a dictionary, but it's not that different from "subclassing" or using the approach I mentioned in the post. I was hoping to "encode"/"decode" Objective-C objects in XML format with a help of the API (either by using NSCoder subclasses or PListSerialization). Maybe I'll have to use a XML parser and handle it all by myself without the plist syntax, which is better).
Eduardo Coelho
I decided not to use .plist anymore and use XML files + some DOM Parser instead. There's a nice tutorial at http://www.raywenderlich.com/725/how-to-read-and-write-xml-documents-with-gdataxml. I realized the plist syntax is not the most appropriate for my needs, but I was expecting the iPhone SDK to help me to make the serialization of my Objective-C Objects (in XML mode) in a straightforward manner, which is not the case.
Eduardo Coelho
A: 

I am ashamed to say that I use [NSString stringWithformat:] to make XML documents. It is the easiest way I have found so far.

sylvanaar
So, you do the work yourself. Representing the object as an XML entity and after decoding (possibly using a XML parser/DOM). I was expecting to serialize my Objective-C objects to a .plist by using NSCoders or PropertyListSerialization. I don't understand why the API is not flexible enough to allow my custom objects to be serialized in XML format if they only have 'property-list' like attributes.
Eduardo Coelho
A: 

Try Excelsior!, an XML Marshaller for Cocoa.

Sophistifunk