我正在尝试使用http://wiki.cs.unh.edu/wiki/index.php/Parsing_XML_data_with_NSXMLParser中描述的方法在 Objective-C 中开发 XML 解析器。
我已经编写了整个流程,但是委托回调方法只是没有响应!
请查看以下代码块,如果您能找出任何错误/错误,请告诉我...
解析器从以下位置调用:
NSString* filePath = [[NSBundle mainBundle] pathForResource:@"cache" ofType:@"xml"];
NSLog(@"Path location is : %@",filePath);
NSData* xmlData = [NSData dataWithContentsOfFile:[NSURL URLWithString:filePath]];
NSXMLParser *nsXmlParser = [[NSXMLParser alloc] initWithContentsOfURL:xmlData];
if(nsXmlParser!=NULL)
{
NSLog(@"parser is %@",nsXmlParser);
}
HDDataXML *parser = [[HDDataXML alloc] initXMLParser];
[nsXmlParser setDelegate:parser];
BOOL success = [nsXmlParser parse];
// test the result
if (success)
{
NSLog(@"No errors - effects count : i");
} else
{
NSLog(@"Error parsing document!");
}
我在这里看到的是错误解析文档!filePath变量正常,解析器不为空。
现在,在委托的 .h 文件中:
#import <Foundation/NSObject.h>
#import "EffectsCache.h"
@class EffectsCache;
@interface HDDataXML : NSObject<NSXMLParserDelegate>
{
EffectsCache *effectsHandler;
NSMutableString *currentElementValue;
NSMutableArray *effects;
}
- (void)parser:(NSXMLParser *)parser
didStartElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI
qualifiedName:(NSString *)qualifiedName
attributes:(NSDictionary *)attributeDict;
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string ;
- (void)parser:(NSXMLParser *)parser
didEndElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI
qualifiedName:(NSString *)qName;
- (HDDataXML *) initXMLParser;
@property (nonatomic, retain) EffectsCache *effectsHandler;
@property (nonatomic, retain) NSMutableArray *effects;
@end
而在.m中的委托实现中:
#import "HDDataXML.h"
@implementation HDDataXML
@synthesize effects, effectsHandler;
- (HDDataXML *) initXMLParser
{
[super init];
effects = [[NSMutableArray alloc] init];
return self;
}
- (void)parser:(NSXMLParser *)parser
didStartElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI
qualifiedName:(NSString *)qualifiedName
attributes:(NSDictionary *)attributeDict
{
NSLog(@"started parsing");
if ([elementName isEqualToString:@"effects"]) {
NSLog(@"effects element found – create a new instance of EffectsCache class...");
effectsHandler = [[EffectsCache alloc] init];
}
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
NSLog(@"mid parsing");
if (!currentElementValue)
{
currentElementValue = [[NSMutableString alloc] initWithString:string];
}
else
{
[currentElementValue appendString:string];
}
NSLog(@"Processing value for : %@", string);
}
- (void)parser:(NSXMLParser *)parser
didEndElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI
qualifiedName:(NSString *)qName
{
NSLog(@"done parsing");
if ([elementName isEqualToString:@"effects"])
{
return;
}
if ([elementName isEqualToString:@"effect"])
{
[effectsHandler addObject:effects];
[effectsHandler release];
effectsHandler = nil;
}
else
{
NSLog(@"cuurent value - %@",currentElementValue);
[effects setValue:currentElementValue forKey:elementName];
}
[currentElementValue release];
currentElementValue = nil;
}
关键是,回调方法不起作用。
请帮我找出错误。
提前致谢。