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?
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.
To learn more about using URL's for communicating with servers read Introduction to the URL Loading System.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With