NSXMLParser returns Error 0 but doesn't parse a file

21 Views Asked by At

The following code:

- (void)ParseFile:(id)sender
{
NSLog(@"Parsing URL: '%@'", FileURL);
NSXMLParser *parser = [[NSXMLParser alloc] initWithContentsOfURL:FileURL];
[parser setDelegate:self];
parser.shouldProcessNamespaces = NO;
if ([parser parse]) {
    NSLog(@"parse successful.");
    [self performSelector:@selector(PostProcessing:) withObject:nil afterDelay:0.1];
    }
else {
    NSError* perror = [parser parserError];
    NSLog(@"parseFailed. Error %ld at line %ld col %ld:", (long)[perror code], (long)[parser lineNumber], (long)[parser columnNumber]);
    NSLog(@" > %@", [perror localizedDescription]);
    NSLog(@" > %@", [perror localizedFailureReason]);
    }
return;
}

When called by:

FileURL = [NSURL fileURLWithPath:@"/Users/me/Documents/Target.xml"];
[self performSelector:@selector(ParseFile:) withObject:nil afterDelay:0.1];

Returns the following:

Parsing URL: 'file:///Users/me/Documents/Target.xml'
parseFailed. Error 0 at line 0 col 0:
  > (null)
  > (null)

(yes, the file exists). But when I call it from the following,

NSOpenPanel* panel = [NSOpenPanel openPanel];
panel.canChooseDirectories = YES;
panel.canChooseFiles = YES;
panel.allowsMultipleSelection = NO;
[panel beginWithCompletionHandler:^(NSInteger result) {
    if (result == 1) {
        FileURL = panel.URL;
        NSLog(@"OnFileOpen:  '%@'", FileURL);
        [self performSelector:@selector(ParseFile:) withObject:nil afterDelay:0.1];
        }
    return;
    }];

and select the same file, it works as expected:

OnFileOpen:  'file:///Users/me/Documents/Target.xml'
Parsing URL: 'file:///Users/me/Documents/Target.xml'
parse successful.

WTF is the difference?

0

There are 0 best solutions below