-1

我正在尝试使用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;
 }

关键是,回调方法不起作用。

请帮我找出错误。

提前致谢。

4

2 回答 2

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];
于 2013-04-09T05:12:07.553 回答
0
 nsXmlParser.delegate = self;
 [nsXmlParser parse];

并且不要忘记在 .h 文件中调用delegate

 <NSXMLParserDelegate>
于 2013-04-08T08:33:11.400 回答