kingston
kingston

Reputation: 1553

webservices using 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.

Upvotes: 1

Views: 348

Answers (3)

PeyloW
PeyloW

Reputation: 36752

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.

Upvotes: 1

nielsbot
nielsbot

Reputation: 16022

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

Upvotes: 2

more tension
more tension

Reputation: 3342

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>

Upvotes: 1

Related Questions