1
votes

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;
}
1
Could you please add NSLog("@"%d", [data length]); before calling initWithData?Sergey Kalinichenko
Does it return 2008 even when you get the error?Sergey Kalinichenko
Yes, the data is always intact even when I get the errorJoe
Oops, that's unexpected: the error code says it's an empty document error.Sergey Kalinichenko
Yes, that is why I am perplexed. I am wondering if anyone knows about the NSXMLParser parse method, and if there are any quirks in its behavior that would cause thisJoe

1 Answers

0
votes

The issue isn't whether data is correct at the beginning of your method objectWithData; it is whether data is correct through out the parse method computation. You should check that integrity of data after the parser completes.

In all likelihood, data is returned by the XMLReader but you are not retaining it properly. Occasionally the computationally intensive parser forces a garbage collection (or pool reclamation) and data gets corrupted.

Just do a [data retain] at the start of your objectwithData method; the problem will then disappear.