Parsing XML Files in iOS

Reading XML files is one of the common tasks we perform in our application, In this tutorial let's see how we can use NSXMLParser to parse XML in our iPhone app.

Introduction

NSXMLParser is a forward only reader or an event driven parser. What it means is, an event is raised whenever the parser comes across a start of an element, value, CDATA and so on. The delegate of NSXMLParser can then implement these events to capture XML data. Some of the events are raised multiple times like the start of an element, value of an element and so on. Since NSXMLParser is known as an event driven parser, we can only read data at the present node and cannot go back. The iPhone only supports NSXMLParser and not NSXMLDocument , which loads the whole XML tree in memory.

Books Application


To understand how to use an instance of NSXMLParser, let's create a simple navigation based application where we will list
the title of the book in the table view and upon selecting a title, display the detail information in a detail view.Create
a new application inXCode by selecting Navigation-Based Application, I have named my app XML. Since the NSXMLParser is a
forward only parser or an event driven parser, we need to store the data locally, which can be used later. To store this data,
we will create a class which replicates the elements and attributes in the XML file. An instance of this class represents one
single Book element in the XML file. I have named this class "Book" and its source code is listed below

//Book.h
#import

@interface Book : NSObject {

NSInteger bookID;
NSString *title; //Same name as the Entity Name.
NSString *author; //Same name as the Entity Name.
NSString *summary; //Same name as the Entity Name.
}

@property (nonatomic, readwrite) NSInteger bookID;
@property (nonatomic, retain) NSString *title;
@property (nonatomic, retain) NSString *author;
@property (nonatomic, retain) NSString *summary;
@end

//Book.m
#import "Book.h"
@implementation Book
@synthesize title, author, summary, bookID;

- (void) dealloc {
[summary release];
[author release];
[title release];
[super dealloc];
}

@end

Notice that the name of the property is the same as the element name in the XML file. Since the XML file has n number of Book elements, we need an array to hold all the books we read, so we declare an array in the application delegate and this is how the source code changes


//XMLAppDelegate.h
#import
@interface XMLAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
UINavigationController *navigationController;
NSMutableArray *books;
}
@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet UINavigationController *navigationController;
@property (nonatomic, retain) NSMutableArray *books;

@end

Reading XML files is one of the common tasks we perform in our application, In this tutorial let's see how we can use NSXMLParser to parse XML in our iPhone app.

Introduction

NSXMLParser is a forward only reader or an event driven parser. What it means is, an event is raised whenever the parser comes across a start of an element, value, CDATA and so on. The delegate of NSXMLParser can then implement these events to capture XML data. Some of the events are raised multiple times like the start of an element, value of an element and so on. Since NSXMLParser is known as an event driven parser, we can only read data at the present node and cannot go back. The iPhone only supports NSXMLParser and not NSXMLDocument, which loads the whole XML tree in memory.

Books Application


To understand how to use an instance of NSXMLParser, let's create a simple navigation based application where we will list the
title of the book in the table view and upon selecting a title, display the detail information in a detail view.

Create a new application in XCode by selecting Navigation-Based Application, I have named my app XML. Since the NSXMLParser is
a forward only parser or an event driven parser, we need to store the data locally, which can be used later. To store this data,
we will create a class which replicates the elements and attributes in the XML file. An instance of this class represents one
single Book element in the XML file. I have named this class "Book" and its source code is listed below

//Book.h
#import

@interface Book : NSObject {
NSInteger bookID;
NSString *title; //Same name as the Entity Name.
NSString *author; //Same name as the Entity Name.
NSString *summary; //Same name as the Entity Name.
}

@property (nonatomic, readwrite) NSInteger bookID;
@property (nonatomic, retain) NSString *title;
@property (nonatomic, retain) NSString *author;
@property (nonatomic, retain) NSString *summary;

@end

//Book.m
#import "Book.h"
@implementation Book
@synthesize title, author, summary, bookID;

- (void) dealloc {
[summary release];
[author release];
[title release];
[super dealloc];
}
@end

Notice that the name of the property is the same as the element name in the XML file. Since the XML file has n number of Book elements, we need an array to hold all the books we read, so we declare an array in the application delegate and this is how the source code changes


//XMLAppDelegate.h
#import

@interface XMLAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
UINavigationController *navigationController;
NSMutableArray *books;
}
@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet UINavigationController *navigationController;
@property (nonatomic, retain) NSMutableArray *books;
@end

This is how the data looks like in the table view and the detail view controller.



The data is then shown in a UITableView with a detail view, the complete code is not shown here but you can download the source code and to get a better understanding UITableView, you can follow my suggested readings below.

0 comments:

Post a Comment