Hello,
Currently I am trying to parse an xml string that I already have (no web calls needed). My app is native iPhone in Objective-C. I have set up an NSXMLParser delegate class which uses initWithData:xmlData. For some reason, the first and only callback on my delegate is to parser: parseErrorOccurred with the following text:
"Unable to download content from web site (Error code 5 )"
Obviously, this makes no sense since I don't ask for anything from the web. Might it still be using some private URL property to call out for something?
Here is some code:
Delegate Class XmlParser:
- (void)parseXmlString:(NSString *)xml parseError:(NSError **)error {
DEBUG_NSLog(@"XML Parser: Called with string: %@", xml);
NSData *xmlData = [xml dataUsingEncoding:NSASCIIStringEncoding];
NSXMLParser *parser = [[NSXMLParser alloc] initWithData:xmlData];
// Set self as the delegate of the parser so that it will receive the parser delegate methods callbacks.
if (parser != nil) {
[parser setDelegate:self];
[parser setShouldProcessNamespaces:NO];
[parser setShouldReportNamespacePrefixes:NO];
[parser setShouldResolveExternalEntities:NO];
[parser parse];
NSError *parseError = [parser parserError];
if (parseError && error) {
*error = parseError;
}
[parser release];
}
}
Called from:
XmlParser *parser = [[XmlParser alloc] init];
NSError *error = nil;
[parser parseXmlString:aString parseError:&error];
if (error) {
DEBUG_NSLog(@"ERROR FROM PARSER");
}
where aString is an NSString containing XML (note: without header).
Error callback that is called:
- (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError {
NSString * errorString = [NSString stringWithFormat:@"Unable to download content from web site (Error code %i )", [parseError code]];
DEBUG_NSLog(@"XML Parser ERROR: %@", errorString);
[parser abortParsing];
}
When the code is run, the parseErrorOccurred hits immediately after [parser parse], and yes, I have implemented each of the didStartDocument, didEndDocument, etc.
Thanks!
UPDATE:
In debugging it seems that the xmlData object that I create is 0 bytes, even though the xml string I pass in to dataUsingEncoding has plenty of data. Is the encoding the issue?
One of the xml elements contains nested html. I'm thinking that the "s and &'s could be a problem. Hopefully doing a "->\" will fix it.
Neither escaping the quotes or replacing any &s with & fixed the problem. Could there be something wrong with having a tag in the string?