nsxmlparserparsing-error

NSXMLParser parsing failure on reuse but no error code


i have 4 NSXMLParser objects.they work fine when i use them for first time but when i use them more than once they fail but return no error code

initialization

 -(void)initParsers
 {
     NSURL* file_url = nil;
        FOR(i,4){       

    file_url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"stack%d",0]ofType:@"xml" inDirectory:@"stackXmls"]];
    stackXmlParser[i] = [[NSXMLParser alloc] initWithContentsOfURL:file_url];
    stackXmlParser[i].delegate = self;
   }   

 }

Parse

BOOL success = [stackXmlParser[currentStack] parse];
if (success)
{
   //Do XML stuff
   ...          
} else {
NSLog(@"Error Error Error!!!");
    NSError *error = [stackXmlParser[currentStack] parserError];
    NSLog(@"Errors with xmlParser: %@  %d", [error localizedDescription], error.code); 
}

OUTPUT

 2012-01-24 16:03:29.542 Cards[5168:207] Error Error Error!!!
 2012-01-24 16:03:29.543 Cards[5168:207] Errors with xmlParser: (null)  0

Solution

  • Try using initWithData instead of initWithContentsOfURL

    NSXMLParser *parser = [[NSXMLParser alloc]initWithContentsOfURL:[NSURL fileURLWithPath:pathToXMLFile]];
    NSLog(@"initWithContentsOfURL 1: %s", ([parser parse])?"OK":"Fail");
    NSLog(@"initWithContentsOfURL 2: %s", ([parser parse])?"OK":"Fail");
    parser = [[NSXMLParser alloc]initWithData:[NSData dataWithContentsOfFile:pathToXMLFile]];
    NSLog(@"initWithData 1: %s", ([parser parse])?"OK":"Fail");
    NSLog(@"initWithData 2: %s", ([parser parse])?"OK":"Fail");
    

    Output

    2012-01-24 18:37:31.260 CLIXMPLParser[12658:707] initWithContentsOfURL 1: OK
    2012-01-24 18:37:31.280 CLIXMPLParser[12658:707] initWithContentsOfURL 2: Fail
    2012-01-24 18:37:31.292 CLIXMPLParser[12658:707] initWithData 1: OK
    2012-01-24 18:37:31.297 CLIXMPLParser[12658:707] initWithData 2: OK