This will be a very simple class that will take our dummy data file, read it in as an NSArray and provide some utility methods to access the data from the file.
@interface Book : NSObject {
NSInteger bookID;
NSString *name; //Same name as the Entity Name.
NSString *address; //Same name as the Entity Name.
NSString *country; //Same name as the Entity Name.
}
@property (nonatomic, retain) NSString *name;
@property (nonatomic, retain) NSString *address;
@property (nonatomic, retain) NSString *country;
@implementation Book
- (void) dealloc {
[country release];
[address release];
[name release];
[super dealloc];
}
@end
#import "XMLAppDelegate.h"
#import "Book.h"
[super init];
appDelegate = (XMLAppDelegate *)[[UIApplication sharedApplication]delegate];
return self;
}
namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName
attributes:(NSDictionary *)attributeDict {
if([elementName isEqualToString:@"Books"]) {
//Initialize the array.
appDelegate.books = [[NSMutableArray alloc] init];
}
else if([elementName isEqualToString:@"Book"]) {
//Initialize the book.
aBook = [[Book alloc] init];
//Extract the attribute here.
aBook.bookID = [[attributeDict objectForKey:@"id"] integerValue];
NSLog(@"Reading id value :%i", aBook.bookID);
}
NSLog(@"Processing Element: %@", elementName);
}
if(!currentElementValue)
currentElementValue = [[NSMutableString alloc]initWithString:string];
else
[currentElementValue appendString:string];
NSLog(@"Processing Value: %@", currentElementValue);
}
namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName {
if([elementName isEqualToString:@"Books"])
return;
//There is nothing to do if we encounter the Books element here.
//If we encounter the Book element howevere, we want to add the book object to the array
// and release the object.
if([elementName isEqualToString:@"Book"]) {
[appDelegate.books addObject:aBook];
[aBook release];
aBook = nil;
}
else
[aBook setValue:currentElementValue forKey:elementName];
[currentElementValue release];
currentElementValue = nil;
}
[aBook release];
[currentElementValue release];
[super dealloc];
}
Book *aBook;
}
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
}
switch(indexPath.section)
{
case 0:
cell.text = aBook.name;
break;
case 1:
cell.text = aBook.address;
break;
case 2:
cell.text = aBook.country;
break;
}
return cell;
}
NSString *sectionName = nil;
switch(section)
{
case 0:
sectionName = [NSString stringWithString:@"Name"];
break;
case 1:
sectionName = [NSString stringWithString:@"Address"];
break;
case 2:
sectionName = [NSString stringWithString:@"Country"];
break;
}
return sectionName;
}
UIWindow *window;
UINavigationController *navigationController;
NSMutableArray *books;
}
@property (nonatomic, retain) IBOutlet UINavigationController*navigationController;
#import "RootViewController.h"
#import "XMLParser.h"
@synthesize navigationController, books;
NSURL *url = [[NSURL alloc]initWithString:@"http://www.edumobile.org/blog/uploads/XML-parsing-data/Data.xml"];
NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithContentsOfURL:url];
//Initialize the delegate.
XMLParser *parser = [[XMLParser alloc] initXMLParser];
//Set delegate
[xmlParser setDelegate:parser];
//Start parsing the XML file.
BOOL success = [xmlParser parse];
if(success)
NSLog(@"No Errors");
else
NSLog(@"Error Error Error!!!");
// Configure and show the window
[window addSubview:[navigationController view]];
[window makeKeyAndVisible];
}
0 comments:
Post a Comment