Xml parsing and serialization for Mac OS X and iPhone OS
Apple provides the NSArchiver and NSUnachriver for object serialization / deserialization, but this can not handle any custom xml schema. So filling an object structure with the data of any custom xml schema has to be made manually. Since the iPhone developer community is rapidly growing, a lot of newbie programmer are despairing to deal with the available xml parsing possibilities.
The iPhone SDK only provides NSXmlParser for xml parsing, which is more useful to read certain parts of an xml file, than filling a whole object structure, which really is a pain.
The other possibility is the famous libxml library, which is written in ANSI C - not easy to use for someone who starts programming with objective-c and never learned proper C before. Event there are a lot of wrappers available, dealing with xml can be a pain for newbies.
And here my idea takes place. An XmlSerializer library which fills an object structure automatically could makes it a lot easier and increase the app quality for many programmers. My Idea should work like this:
The xml file:
<Test name="Michael" uid="28">
<Adress street="AlphaBetaGammastrasse 1" city="Zürich" postCode="8000" />
<Hobbies>
<Hobby describtion="blabla"/>
<Hobby describtion="blupblup"/>
</Hobbies>
</Test>
The business objects:
@interface Test : NSObject {
NSString *name;
Adress *adress;
NSArray *hobbies;
int uid;
}
@property (nonatomic, copy) NSString *name;
@property (nonatomic, retain) Adress *adress;
@property (nonatomic, retain) NSArray *hobbies;
@property (nonatomic, readwrite) int uid;
@end
@interface Adress : NSObject {
NSString *street;
NSString *city;
int postCode;
}
@property (nonatomic, copy) NSString *street;
@property (nonatomic, copy) NSString *city;
@property (nonatomic, readwrite) int postCode;
@end
How the xml serializer should work:
XMLSerializer *serializer = [[XMLSerializer alloc] init]; NSData *data = [NSData dataWithContentsOfFile:@"~/Documents/Test.xml"]; Test *test = [serializer deserializeWithData:data];
So I started a project for this idea. If you are interested in, the project can be found here. If you would like to join the project, so don’t hesitate to use the provided mailing list and ask for access.
4 Comments to Xml parsing and serialization for Mac OS X and iPhone OS
What is the status of the project? I checked out the latest version from SVN but would appear much of the code is commented out and therefore not really working.
Since I’m very busy with my job at the moment, I don’t have much time for continuing, but I will finish it as soon as possible.
I’ll take some of your suggestions and try to apply them.
May 5, 2010
It would be great if the example you posted here works. I badly need one of the serializers. Your code does not work. So, I am sticking to a simple idea of using NSXML sax parser, pushing created objects into a stack and popping them out to make a collection of serialized objects.
Please, can u post a working code.?
August 20, 2009