O'Reilly logo

iOS 4 Programming Cookbook by Vandad Nahavandipoor

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Chapter 6. Networking and XML

6.0. Introduction

iOS applications can take advantage of iOS SDK networking and XML parsing classes in order to create richer and connected applications. Whether you are trying to download a new resource from the Internet into your application’s bundle or make a call to a web service, you can find various classes and functionalities in the SDK that will help you make the process much easier than you thought. The iOS SDK provides the NSXMLParser class to parse XML and the NSURLConnection class (among others) for web services.

In this chapter, we will go through the implementation and usage of these classes, and then mix them to parse remote XML files. We will also discuss caching mechanisms and how to implement a custom mechanism for specific needs.

6.1. Opening and Reading a Local XML File

Problem

You want to read the contents of an XML file that has been read into memory, perhaps into an instance of the NSData class.

Solution

Create an instance of NSXMLParser and provide it with a delegate that conforms to the NSXMLParser protocol:

  NSXMLParser *parser = [[NSXMLParser alloc] initWithData:nil];
  [parser setDelegate:self];
  [parser parse];

In this code, we are passing nil to the initWithData: method of the NSXMLParser. In this recipe’s Discussion, you will see how to load the contents of an XML file from our application bundle into an instance of NSData and pass it to the NSXMLParser to parse.

Discussion

Instances of the NSXMLParser class are able to parse XML documents. ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required