简体   繁体   中英

NSXMLParserDelegate Methods Not Being Called

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?

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];
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM