开发者

"Unable to download content from web site" while NSXMLParser initWithData

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 somethin开发者_运维知识库g wrong with having a tag in the string?


Your error message is hiding the actual error. Your xmlstring appears to be invalid as the error code is "Error code 5". See this other SO question. NSXMLparser errorcode 5

Update

When creating your xmlData instance use NSUTF8StringEncoding instead of NSASCIIStringEncoding If that stil fails, post the actual string. Passing an empty data object to the parser is causing the error.


I tried above code with a sample XML DATA - it works great. It look like there is some issue with XML data you pass to the function.

Check your XML data or share your xml input for further analysis...


You cannot use <> characters in xml. Replace them with:

< = &lt;

> = &gt;


When dealing with XML the first parsing error is always fatal. If there is a parsing error, its not valid XML.

You should encode the raw HTML into HTML entities. Having raw HTML (from a user or third party source) zipping around in an app is considered a Bad Idea™.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜