Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xml serialization library for iPhone Apps [closed]

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 classes to fill

@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

NSError *error = nil;
XMLSerializer *serializer = [[XMLSerializer alloc] init];
NSData *data = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"TestFile" ofType:@"xml"]];
Test *test = [serializer deserializeWithData:data error:&error];

To fill the object structure needs only one line of code:

Test *test = [serializer deserializeWithData:data error:&error];

This would be so easy to use that any newbie programmer could use it. For more advanced usage the serializer could be configurable.

What do you think, would this be a helpful and popular library for iPhone and OSX Applications?

Edit: You can see the project here, but it is fare away from release.

like image 621
Enyra Avatar asked Jun 03 '09 12:06

Enyra


People also ask

What is XML serialization?

XML serialization is the process of converting XML data from its representation in the XQuery and XPath data model, which is the hierarchical format it has in a Db2® database, to the serialized string format that it has in an application.

How to ignore property in XML serialization c#?

To override the default serialization of a field or property, create an XmlAttributes object, and set its XmlIgnore property to true . Add the object to an XmlAttributeOverrides object and specify the type of the object that contains the field or property to ignore, and the name of the field or property to ignore.

What is XmlIgnoreAttribute?

The XmlIgnoreAttribute belongs to a family of attributes that controls how the XmlSerializer serializes or deserializes an object. If you apply the XmlIgnoreAttribute to any member of a class, the XmlSerializer ignores the member when serializing or deserializing an instance of the class.

Is a Namespsace for XML serialization?

XML namespaces provide a way to qualify the names of XML elements and attributes in XML documents. A qualified name consists of a prefix and a local name, separated by a colon. The prefix functions only as a placeholder; it is mapped to a URI that specifies a namespace.


1 Answers

The NSKeyedArchiver works precisely because it doesn't try to map onto an XML schema. Many, many XML schemas are badly designed (i.e. they're translating an in-memory object structure to an external representation format). The key problem is that the documents should be designed to make sense from a document perspective, and that that would then need to map onto whatever memory layout you wanted for your objects. Ever seen XML documents with lots of 'refid' attributes referring to other parts of the doc? Those are usually transliterated from a relational database which is just sticking angled brackets on the resultset.

So starting by assuming a one-to-one mapping between an XML document and its code representation is pretty much doomed in all but the simplest cases. Just consider where we would be today with HTML if it had been designed around the C++ objects that were used to instantiate the document in the first browser ... (well, more like Objective-C, but hey ...)

The point about NSKeyedArchiver is that you can evolve the data structure without breaking the ability to load older versions. It's unbelievably difficult to do that (properly) using some kind of automated instance-var-to-element mapping.

like image 87
AlBlue Avatar answered Sep 25 '22 15:09

AlBlue