@synthesize xmlView, xmlData, xmlList, currentItem, xmlValue;


- (void)viewDidLoad {

self.xmlData = [[NSData alloc] init];

self.xmlList = [[NSMutableArray alloc] init];

self.xmlValue = [[NSMutableString alloc] init];

self.currentItem = [[NSMutableDictionary alloc] init];

NSString *path = [[NSBundle mainBundle] pathForResource:@"Sample" ofType:@"xml"];

NSData *data = [[NSData alloc] initWithContentsOfFile:path];

self.xmlData = data;

NSXMLParser *parser = [[NSXMLParser alloc] initWithData:self.xmlData];

[parser setDelegate:self];

[parser parse];

[parser release];

[xmlView reloadData];

[super viewDidLoad];

}

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

{

if ([elementName isEqualToString:@"d:entry"]) {

elementType = etItem;

}

[self.xmlValue setString:@""];

}


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

{

if(elementType == etItem){

[xmlValue appendString:string];

}

}


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

{

if(elementType != etItem)

return;

if([elementName isEqualToString:@"d:entry"]){

[self.xmlList addObject:[NSDictionary dictionaryWithDictionary:self.currentItem]];

}

else if([elementName isEqualToString:@"d:index"]){

[self.currentItem setValue:[NSString stringWithString:self.xmlValue] forKey:elementName];

}

else if([elementName isEqualToString:@"h1"]){

[self.currentItem setValue:[NSString stringWithString:self.xmlValue] forKey:elementName];

}

else if([elementName isEqualToString:@"p"]){

[self.currentItem setValue:[NSString stringWithString:self.xmlValue] forKey:elementName];

}

}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil) {

cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];

}

NSUInteger row = [indexPath row];

NSDictionary *dt = [self.xmlList objectAtIndex:row];

cell.textLabel.text = [NSString stringWithFormat:@"%@-%@", [dt objectForKey:@"h1"], [dt objectForKey:@"p"]];

return cell;

}


'Object-C' 카테고리의 다른 글

MoviePlayer  (0) 2010.10.09
MapView  (0) 2010.10.09
XML  (0) 2010.10.09
SQLITE  (0) 2010.10.08
CoreData  (0) 2010.10.07
archiving  (0) 2010.10.07

+ Recent posts