2

我以前做过一个 NSXML Parser,但是这次由于某种原因,当我调用parse.

这就是我所说的解析:

- (void)parseXMLFile:(NSString *)pathToFile 
{
    NSURL* xmlURL = [NSURL fileURLWithPath:pathToFile];
    _parser = [[NSXMLParser alloc] initWithContentsOfURL:xmlURL];
    [_parser setDelegate:self];
    [_parser parse]; 
}

这是我的委托方法:

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

这是我的标题:

@interface Engine : NSObject <NSCoding, NSXMLParserDelegate>

我的 XML 文件是项目的一部分,所以我不知道为什么不调用委托方法。有任何想法吗?

4

1 回答 1

1

这是我的路径名。我将我的更改parseXMLFile为:

- (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];
}
于 2011-12-28T16:55:33.257 回答