Reputation: 53
I've done an NSXML Parser before, but for some reason this time my NSXMLParserDelegate methods aren't being called when I call parse
.
Here's where I call parse:
- (void)parseXMLFile:(NSString *)pathToFile
{
NSURL* xmlURL = [NSURL fileURLWithPath:pathToFile];
_parser = [[NSXMLParser alloc] initWithContentsOfURL:xmlURL];
[_parser setDelegate:self];
[_parser parse];
}
Here's my delegate methods:
#pragma mark NSXMLParserDelegate Methods
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict
{
if ([elementName isEqualToString:@"section"])
{
_currentSection = [[Section alloc] init];
_currentSection.name = [attributeDict valueForKey:@"title"];
}
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
if([elementName isEqualToString:@"section"])
{
[_sections addObject:_currentSection];
[_currentSection release];
}
}
And here's my header:
@interface Engine : NSObject <NSCoding, NSXMLParserDelegate>
My XML files are part of the project, so I can't figure out why the delegate methods wouldn't be getting called. Any ideas?
Upvotes: 2
Views: 2007
Reputation: 53
It was my pathname. I changed my parseXMLFile
to:
- (void)parseXMLFile:(NSString *)pathToFile
{
NSString * filename = [pathToFile stringByDeletingPathExtension];
NSString * extension = [pathToFile pathExtension];
// Get file contents
NSData * data = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:filename ofType:extension]];
_parser = [[NSXMLParser alloc] initWithData:data];
[_parser setDelegate:self];
[_parser parse];
}
Upvotes: 1