I am using the class XMLReader to parse some XML from a URL. The XML is successfully parsed sometimes, and sometimes I get:
Error Domain=NSXMLParserErrorDomain Code=4 "The operation couldn’t be completed. (NSXMLParserErrorDomain error 4.)"
The parse is usually successful the first time I run it, after changing something, and it fails after that until I change something else. For example, in the code below, I tried commenting out the [parser release] line, and it parsed successfully. Then I ran it again and back to error code 4.
I log the same input data every time, success or fail.
Any ideas what is going wrong here? I can paste in more code if that would help, but I have isolated the error to be within the NSXMLParser parse method (called in the code below), because it always receives the same data.
Thanks!
edit: I know that error code 4 is an empty document error. But I know my NSData is not empty. So there is something else happening here
- (NSDictionary *)objectWithData:(NSData *)data
{
//data always makes it here, the same data gets logged regardless of parse success
//NSLog(@"%@",data);
// Clear out any old data
[dictionaryStack release];
[textInProgress release];
dictionaryStack = [[NSMutableArray alloc] init];
textInProgress = [[NSMutableString alloc] init];
// Initialize the stack with a fresh dictionary
[dictionaryStack addObject:[NSMutableDictionary dictionary]];
// Parse the XML
NSXMLParser *parser = [[NSXMLParser alloc] initWithData:data];
parser.delegate = self;
[parser setShouldResolveExternalEntities:NO];
BOOL success = [parser parse];
[parser release];
// Return the stack's root dictionary on success
if (success)
{
NSDictionary *resultDict = [dictionaryStack objectAtIndex:0];
return resultDict;
}
return nil;
}
NSLog("@"%d", [data length]);
before callinginitWithData
? – Sergey Kalinichenko