Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parsing XML from an HTTPS URL using NSXMLParser?

I'm trying to parse XML directly from an HTTPS URL, as follows:

NSString *const URL = @"https://some/HTTPS/url";
NSURL* url = [NSURL URLWithString:URL];
NSXMLParser* parser = [[NSXMLParser alloc] initWithContentsOfURL:url];
[parser setDelegate:self];
[parser parse];

I have the following delegate method for the parser:

- (void) parser:(NSXMLParser *)parser 
didStartElement:(NSString *)elementName 
namespaceURI:(NSString *)namespaceURI 
qualifiedName:(NSString *)qualifiedName 
attributes:(NSDictionary *)attributeDict
{
    NSLog(@"Started parsing %@", elementName);
}

This seems to work fine for HTTP URL's, but shows no result for an HTTPS URL.

How could I fix this?

like image 443
Debajit Avatar asked Dec 14 '22 05:12

Debajit


2 Answers

  1. None of the initWithContentsOfURL:... methods will allow you to answer the authentication message from the https server. So look at NSURLConnection and NSURLDownload which have delegate messages that help you handle authentication.

  2. To learn more about using URL's for communicating with servers read Introduction to the URL Loading System.

  3. As far as parsing HTML with an XML parser, it will only reliably work with XHTML. So if you are creating and parsing your own XHTML files that should work in most cases. But if you are loading any HTML file from the internet then the XML parser will often not be able to parse the file. You may want to look at WebKit for that.

like image 105
Nathan Kinsinger Avatar answered Dec 23 '22 23:12

Nathan Kinsinger


You should use NSURLConnection to download the XML data and then parse the output rather than using -initWithContentsOfURL:.

NSURLConnection is more robust and also allows you to do asynchronous fetching which you should definitely be doing, -initWithContentsOfURL: will block the main thread.

like image 42
Rob Keniger Avatar answered Dec 23 '22 22:12

Rob Keniger