NSXMLParser

NSXMLParser is often used for parsing XML files in iPhone/iPad application. Here is XML sample file…

<People>
<Person id=”1″>
<firstname>Luka</firstname>
<lastname>Gabrić</lastname>
<adress>Keršovanijeva 4</adress>
<contact>[email protected]</contact>
</Person>
<Person id=”2″>
<firstname>Ivan</firstname>
<lastname>Kalaica</lastname>
<adress>Keršovanijeva 4</adress>
<contact>[email protected]</contact>
</Person>
<Person id=”3″>
<firstname>Ana</firstname>
<lastname>Gabrić</lastname>
<adress>Keršovanijeva 4</adress>
<contact>[email protected]</contact>
</Person>
</People>

First it is necessary to create Class Person that will contain subelements of XML file. The names of objects in the Class Person are the same as the names of elements in the XML file.

//Person.h

#import <UIKit/UIKit.h>

@interface Person : NSObject
{

NSInteger personID;
NSString *firstname;
NSString *lastname;
NSString *adress;
NSString *contact;
}

@property (nonatomic, readwrite) NSInteger personID;
@property (nonatomic, retain) NSString * firstname;
@property (nonatomic, retain) NSString * lastname;
@property (nonatomic, retain) NSString *adress;
@property (nonatomic, retain) NSString *contact;

@end

//Person.m

#import “Person.h”

@implementation Person

@synthesize firstname, lastname, adress, contact, personID;

– (void) dealloc
{
[firstname release];
[lastname release];
[adress release];
[contact release];
[super dealloc];
}

@end

XMLParser Class contains current value of the element, an object of type XMLAppDelegate and an object of type Person. This class will also contain a reference to applications delegate.

//XMLParser.h

#import <UIKit/UIKit.h>

@class XMLAppDelegate, Person;

@interface XMLParser : NSObject
{
NSMutableString *currentElementValue;
XMLAppDelegate *appDelegate;
Person *aPerson;
}

– (XMLParser *) initXMLParser;

@end

//XMLParser.m

– (XMLParser *) initXMLParser
{
[super init];
appDelegate = (XMLAppDelegate *)[[UIApplication sharedApplication] delegate];
return self;
}

People from the XML file will be added to array – people which is declared in XMLAppDelegate Class. When application is launched NSURL instance and NSXMLParser instance are created. The delegate is set up and reading of XML file begins.

//XMLAppDelegate.h

#import <UIKit/UIKit.h>

@interface XMLAppDelegate : NSObject <UIApplicationDelegate>
{
UIWindow *window;
NSMutableArray *people;
}

//XMLAppDelegate.m

– (void)applicationDidFinishLaunching:(UIApplication *)application
{
NSURL *url = [[NSURL alloc] initWithString:peopleLink];
NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithContentsOfURL:url];
XMLParser *parser = [[XMLParser alloc] initXMLParser];
[xmlParser setDelegate:parser];

//Reading begins
BOOL success = [xmlParser parse];
if (success)
NSLog(@”OK”);
else
NSLog(@”ERROR”);

[window addSubview:[navigationController view]];
[window makeKeyAndVisible];
}

When the parser reaches an element method:

– (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName
attributes:(NSDictionary *)attributeDict

is triggered. In which array – people or object – person are created (depending on the element name). Following method triggered is:

– (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string

In which current value of the element is set. When parser reaches the end of the element method:

– (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName

is triggered. Parser will trigger these three methods until it reaches end of file.

//XMLParser.m

– (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName
attributes:(NSDictionary *)attributeDict
{
if ([elementName isEqualToString:@”People”])
appDelegate.people = [[NSMutableArray alloc] init];
else if ([elementName isEqualToString:@”Person”])
{
aPerson = [[Person alloc] init];
aPerson.personID = [[attributeDict objectForKey:@”id”] integerValue];
}
}

– (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
if(!currentElementValue)
currentElementValue = [[NSMutableString alloc] initWithString:string];
else
[currentElementValue appendString:string];
}

– (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
if([elementName isEqualToString:@”People”])
return;
if([elementName isEqualToString:@”Person”])
{
[appDelegate.people addObject:aPerson];
[aPerson release];
aPerson = nil;
}
else
[aPerson currentElementValue forKey:elementName];

[currentElementValue  release];
currentElementValue  = nil;
}

Result: