views:

76

answers:

4

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?

+1  A: 

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...

Girish Kolari
Thanks. Please see update.
TahoeWolverine
the present issue is XML data encoding issue, value part should not have '<' or '>' otherwise encode xml data appropriately ...
Girish Kolari
'\' is part of XML tag structure you can not use to escaping.
Girish Kolari
for xml encoding refer the link http://groups.google.com/group/google-checkout-developers-forum/web/encoding-special-characters-in-xml
Girish Kolari
+1  A: 

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. http://stackoverflow.com/questions/2651033/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.

falconcreek
Thanks, looking into it...
TahoeWolverine
No problem. BTW the "download error" is coming from this line. NSString * errorString = [NSString stringWithFormat:@"Unable to download content from web site (Error code %i )", [parseError code]]; 
falconcreek
Yes, thank you. Please see update and let me know your take.
TahoeWolverine
Updated answer. The empty data instance is causing the error.
falconcreek
+1  A: 

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

< = &lt;

> = &gt;
willcodejavaforfood
How would I distinguish between the <>'s in the xml and the <>'s in the html?
TahoeWolverine
+1  A: 

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™.

ExitToShell
Hey thanks for the encoding tip. Do you have a snippet of what you're talking about with entities, or do you just mean doing what falconcreek suggested?
TahoeWolverine
worry about the entities once you are able to pass a valid data object to the parser.
falconcreek