我正在尝试使用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;
}
重点是,回调方法不起作用。
请帮我找到错误。
提前致谢。
答案 0 :(得分:0)
nsXmlParser.delegate = self;
[nsXmlParser parse];
并且不要忘记在.h文件中调用delegate
<NSXMLParserDelegate>
答案 1 :(得分:0)
好的......我终于解决了它:
正如Martin R所说,我更改了调用委托的代码:
NSString* filePath = [[NSBundle mainBundle] pathForResource:@"effects_cache" ofType:@"xml"];
NSLog(@"Path location is : %@",filePath);
NSData* xmlData = [NSData dataWithContentsOfFile:filePath];
NSXMLParser *nsXmlParser = [[NSXMLParser alloc] initWithData:xmlData];
在进行此修改之前, xmlData 为空。
需要另外一个小修改: 更改: [effectsHandler addObject:effects];
要
[effects setValue:currentElementValue forKey:elementName];