我以前做过NSXML Parser,但由于某种原因,这次我调用parse
时没有调用我的NSXMLParserDelegate方法。
这是我称之为解析的地方:
- (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文件是项目的一部分,所以我无法弄清楚为什么委托方法不会被调用。有什么想法吗?
答案 0 :(得分: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];
}