开发者

NSXMLParser initWithContentsOfURL - Parse a GZIP'd Response

I have an iOS app that is calling a web service for some XML data as such:

NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithContentsOfURL:xmlURL];
[xmlParser setDelegate:self];
[xmlParser setShouldProcessNamespaces:NO];
[xmlParser setShouldReportNamespacePrefixes:NO];
[xmlParser setShouldResolveExt开发者_如何学CernalEntities:NO];        
[xmlParser parse];

I kept getting an error:

Error parsing XML: The operation couldn’t be completed. 
(NSXMLParserErrorDomain error 31.)

Error 31 is "NSXMLParserUnknownEncodingError". I sniffed the HTTP response in WireShark and it turns out the web service is sending "Content-Encoding: gzip" with GZIP'd data in the body and the reason it's doing that is because the request being sent from iOS sets "Accept-Encoding: gzip, deflate" in the HTTP headers. It seems the initWithContentsOfURL tells webservers it can handle gzip'd data when it actually can't.

I'm guessing I need to load the web server response into NSData then run it through a decompressor method to get a NSString and then pass that string to the NSXMLParser.

My question is, what's the best way to do this decompression? ZLib? Any issues linking to ZLib from within iOS?

Thank you,

Stateful

P.S. I tagged this with osx too since I imagine this problem would be there as well, since NSXMLParser is a Foundation class.


Use NSURLConnection delegate methods to download response from webservice. then pass the data to NSXMLParser initWithContentsOfData. By using NSURLConnection delegate methods (asynchronous connection) you are not blocking the current thread. This is recommended way to download response. Plus NSURLConnection uncompressses(deflates) g-zip compressed response. you don't need to do anything special to deflate it. And you can inspect the data before giving it to NSXMLParser ( Thanks Steven ).

Using NSXMLParser initWithContentsOfURL should be mostly used to parse XML files within your Application (in your resources or documents sections). It surely makes connection with your webservice to download response BUT it is equivalent to synchronous call and blocks your thread. Avoid NSURLConnection sendSynchronous and initWithContentsOfURL to pass remote webservice URL.


NSURLConnection handles gzip decompression (i.e. download) automatically. iOS specifically asks for gzip content in the HTTP header. It can handle it.

The synchronous network transaction is a problem, though. Instead, you should use an asynchronous request to get the data. When the data is finished, you can pass it on to NSXMLParser. Another benefit to this is that you'll be able to inspect the data, which you'll probably find isn't really XML.

As a quick hack to see what's going on, I believe there's an NSData method for downloading from a URL synchronously. That data instance could then be fed to NSXMLParser. But this is a temporary fix. You don't want to leave any synchronous networking in your application; if it takes too long, the watchdog will kill your app dead. But it'll likely point out what's wrong with your XML.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜