适用于iPhone应用程序的Xml序列化库 [英] Xml serialization library for iPhone Apps

查看:126
本文介绍了适用于iPhone应用程序的Xml序列化库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Apple为对象序列化/反序列化提供了NSArchiver和NSUnachriver,但是它无法处理任何自定义xml架构。因此,必须手动填充具有任何自定义xml架构的数据的对象结构。
由于iPhone开发者社区正在快速增长,许多新手程序员都在绝望地处理可用的xml解析可能性。

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.

iPhone SDK仅提供NSXmlParser对于xml解析,这对于读取xml文件的某些部分比填充整个对象结构更有用,这真的很痛苦。

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.

另一种可能性是着名的libxml库,用ANSI C编写 - 对于那些开始使用objective-c进行编程并且之前从未学过适当的C的人来说不容易使用。事件中有很多可用的包装器,处理xml对新手来说可能是一种痛苦。

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.

这里我的想法发生了。自动填充对象结构的XmlSerializer库可以使许多程序员更轻松地提高应用程序质量。
我的意见应该是这样的:

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:

xml文件

<Test name="Michael" uid="28">
    <Adress street="AlphaBetaGammastrasse 1" city="Zürich" postCode="8000" />

  <Hobbies>
    <Hobby describtion="blabla"/>
    <Hobby describtion="blupblup"/>
  </Hobbies>
</Test>

要填写的课程

@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

xml序列化程序应如何工作

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.

您认为这对iPhone和OSX应用程序来说是一个有用且流行的库吗?

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

编辑:
您可以看到该项目此处,但它远离发布。

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

推荐答案

NSKeyedArchiver正是因为它不会尝试映射到XML而正常工作架构。许多XML模式设计得很糟糕(即它们将内存中的对象结构转换为外部表示格式)。关键问题是文档应该从文档的角度来理解,然后需要映射到对象所需的任何内存布局。曾经见过的XML文档中有很多refid属性引用了doc的其他部分吗?这些通常是从关系数据库中音译出来的,该数据库只是在结果集上插入了斜角括号。

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.

首先假设XML文档与其之间的一对一映射除了最简单的情况之外,代码表示几乎注定要失败。如果它是围绕用于在第一个浏览器中实例化文档的C ++对象设计的,那么只需考虑我们今天使用HTML的位置......(好吧,更像是Objective-C,但是嘿......)

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 ...)

关于NSKeyedArchiver的一点是,您可以在不破坏加载旧版本的能力的情况下改进数据结构。使用某种自动化的实例变量到元素映射(正确地)这是令人难以置信的困难。

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.

这篇关于适用于iPhone应用程序的Xml序列化库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆