使用 plist 的网络服务

发布于 2024-11-06 14:23:03 字数 203 浏览 1 评论 0原文

我一直在使用 NSXMLParser 来解析 xml 并成功启动应用程序...最近我遇到了 xml 的问题...因为 xml 很重。它增加了内存开销...我计划转向 plist (因为它大大减少了内存开销)。并且不知道从哪里开始...我已经在整个网络中进行了搜索,但没有成功,你们能给我一个示例代码,甚至是包含 plist 的工作网址吗...已经足够了...我如果你们能帮助我,我将非常感激。

I've been using NSXMLParser for parsing xml and was successful in launching apps ... Recently I'm facing problems with xml ... Since xml is heavy weight. It increases the memory over head ... I'm planning to shift to plist(since it reduces the memory overhead considerably). And have no idea where to begin with ... I've searched all through the net and was not successful could u guys gimme me a sample code or even a working url containing a plist ... Is more than enough ... I'd be so grateful if u guys could help me out.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

野稚 2024-11-13 14:23:03

如果您可以控制数据的格式,您是否考虑过使用 JSON 来代替?

if you have control over the format of the data, have you considered using JSON instead?

骑趴 2024-11-13 14:23:03

您可以使用 https://github.com/jayway/CWFoundationCWXMLTranslator > 轻松从 XML 转换为域对象。

假设您有这个域类:

@interface Book : NSObject {
}
@property(copy) NSString* author;
@property(copy) NSString* title;
@end

和这个 XML:

<books>
  <book>
    <author>James Joyce</author>
    <title>Ulysses</title>
  </book>
  <!-- MORE BOOKS HERE -->
</book>

您只需定义一个翻译文件,将其命名为 Book.xmltranslation,并添加以下内容:

book +> @root : Book {
   author >> author;
   title >> title;
}

然后,它将用于从该文件中获取和转换 XML服务器到 Book 类的实时实例中,如下所示:

NSArray* books = [CWXMLTranslator translateContentsOfURL:url
                                    withTranslationNamed:@"Book"
                                                delegate:nil
                                                   error:NULL];

这是最简单的用例,如果需要,甚至可以内联编写翻译 DSL。 CWXMLTranslator 还支持更复杂的操作,例如日期、URL、数字等的类型转换,以及嵌套类型,以及直接转换为 Core Data 托管对象。

CWFoundation 项目包含您需要的所有文档以及解析 RSS 源的示例项目。

You can use the CWXMLTranslator from https://github.com/jayway/CWFoundation for easy translation from XML to domain object.

Assume you have this domain class:

@interface Book : NSObject {
}
@property(copy) NSString* author;
@property(copy) NSString* title;
@end

And this XML:

<books>
  <book>
    <author>James Joyce</author>
    <title>Ulysses</title>
  </book>
  <!-- MORE BOOKS HERE -->
</book>

You only need to define a translation file, name it Book.xmltranslation, and add this:

book +> @root : Book {
   author >> author;
   title >> title;
}

That would then be used to fetch and trabnslate the XML fromt he server into live instances of your Book class like this:

NSArray* books = [CWXMLTranslator translateContentsOfURL:url
                                    withTranslationNamed:@"Book"
                                                delegate:nil
                                                   error:NULL];

This is the easiest usecase available, the translation DSL can even be written inline if you want. The CWXMLTranslator support much more complex operations also, like type convertions to dates, URLs, numbers, etc. As well as nested types, and direct translation to Core Data managed objects.

The CWFoundation project contains all documentation you need, and a sample project that parses RSS feeds.

指尖凝香 2024-11-13 14:23:03

现代 plist XML:属性列表。可以使用和创建二进制 plist,但它们只是二进制 XML 的一种形式。这反过来意味着开销会有所减少,但会牺牲可读性。 JSON 可以小于等效的 XML,但并非总是如此。

抛开所有这些不谈,plist 往往比同等的 XML 更冗长。将 XML:

<book>
    <author>James Joyce</author>
    <title>Ulysses</title>
</book>

与等效的 plist 进行比较:

<dict>
    <key>author</key>
    <string>James Joyce</string>
    <key>title</key>
    <string>Ulysses</string>
</dict>

Modern plists are XML: Property Lists. It's possible to use and create binary plists, but they're just a form of binary XML. That can in turn mean some reduction in overhead, but at the cost of readability. JSON can be smaller than equivalent XML, though not always.

Leaving all that aside, plists tend to be more verbose than equivalent XML. Compare the XML:

<book>
    <author>James Joyce</author>
    <title>Ulysses</title>
</book>

with an equivalent plist:

<dict>
    <key>author</key>
    <string>James Joyce</string>
    <key>title</key>
    <string>Ulysses</string>
</dict>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文