iPhone 中的 XML 解析模式
我有一个网络服务,它返回某些“模型”,其中所有模型都是由 Objective-C 中的类定义的。对 RESTful 方法的调用将返回单个模型 XML 或模型 XML 元素列表。
<widget>
<a>foo</a>
</widget>
或者
<widgets>
<widget>
<a>foo</a>
</widget>
....
<widget>
<a>foo</a>
</widget>
</widgets>
我试图想出一种方法来组织我的类,以便解析对象或对象列表很容易,并且最终添加更多模型对象也很容易。显然,每个“模型”都会有一个代表,但是如何以一种既简单又优雅的方式组织它呢?每个模型对象的 xml 委托?如何处理列表?也许某种类型的列表委托对象将根据某个列表引用正确的单个模型委托? (即:小部件 -> 小部件委托)
我将有一个类,它提供 Web 服务中的所有方法。
- (Widget *)getWidgetById: (int) id;
- (some array) getWidgets:;
我想这实际上更像是一个面向对象的设计模式问题,而不是其他问题。
I have a web service which returns certain "models" of which are all defined by a class in objective-c. Calls to RESTful methods will return either a singular model XML or a list of model XML elements.
<widget>
<a>foo</a>
</widget>
or
<widgets>
<widget>
<a>foo</a>
</widget>
....
<widget>
<a>foo</a>
</widget>
</widgets>
I'm trying to come up with a way to organize my classes in such a way that parsing the object or list of objects is easy and ultimately adding more model objects is easy. It will obviously involve a delegate for each "model", but how do you organize it in such a way that makes it easy and elegant. A xml delegate for each model object? how to handle the lists? Perhaps some type of list delegate object which will then refer to the correct individual model delegate according to some list? (ie: widgets -> widget delegate)
I am going to have a single class which provides all the methods in the webservice.
- (Widget *)getWidgetById: (int) id;
- (some array) getWidgets:;
I guess its really more of a OO design pattern question then anything else.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可能需要首先查看 XMLPerformance Apple 示例代码。您可以选择使用 <代码>NSXMLParser或
libxml
,或者只是推出您自己的解决方案。另请参阅 XML 解析部分 ="https://developer.apple.com/iphone/library/codinghowtos/DataManagement" rel="nofollow noreferrer">数据管理编码指南。You might want to start by looking at the XMLPerformance Apple sample code. You may choose to use
NSXMLParser
orlibxml
, or just roll your own solution. Also see the XML parsing section of the Data Management Coding How-To's.如果我是你,我会强烈考虑让服务器返回 JSON 或 PLIST。
这两种格式的优点是,它们不像 XML 那样开放,因此您可以自然地将事物构造为数组、字典和各种类型的元素,这些元素自然映射回对象属性并且非常容易解析(对于 JSON,您需要像 TouchJSON 这样的库,对于 plist,您可以使用内置解析)。您也不用担心何时将某些东西设为元素或属性。
基本上,公式是这样的:您从服务器获得响应,在一次调用中将数据转换为 NSDictionary 或 NSArray(通常是 NSDictionaries 数组),然后将其中的属性提取到您的数据模型中。
If I were you I'd strongly consider having the server return either JSON or PLISTs.
The advantage of those two formats is, that they are less open-ended than XML and so you naturally structure things into arrays, dictionaries, and variously typed elements which naturally map back into object properties and are very easy to parse (for JSON you need a library like TouchJSON, for plists you can use the built-in parsing). You also are not worrying about when to make something an element vs. an attribute.
Basically the formula is this: you get a response from the server, in one call turn the data into an NSDictionary or NSArray (usually an array of NSDictionaries) and then just pull out attributes from them into your data model.