title: Streaming NSXMLParser with NSInputStream
body: <p><strong>Update:</strong></p>
  
  <p>When using <code>NSXMLParser</code> class method <code>initWithContentsOfURL</code>, rather than parsing as the XML feed is downloaded, it appears to try to load the entire XML file into memory, and only then initiate the parsing process. This is problematic if the XML feed is large (using an excessive amount of RAM, inherently inefficient because rather than parsing in parallel with the download, it only starts the parsing once the download is done, etc.).</p>
  
  <p>Has anyone discovered how to parse as the feed is being streamed to the device using <code>NSXMLParser</code>? Yes, you can use <code>LibXML2</code> (as discussed below), but it seems like it should be possible to do it with <code>NSXMLParser</code>. But it's eluding me.</p>
  
  <p><strong>Original question:</strong></p>
  
  <p>I was wrestling with using <a href="https://developer.apple.com/library/ios/#documentation/Cocoa/Reference/Foundation/Classes/NSXMLParser_Class/Reference/Reference.html" rel="nofollow"><code>NSXMLParser</code></a> to read XML from a web stream. If you use <a href="https://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Classes/NSXMLParser_Class/Reference/Reference.html#//apple_ref/occ/instm/NSXMLParser/initWithContentsOfURL%3a" rel="nofollow"><code>initWithContentsOfURL</code></a>, while the interface may lead one to infer that it would stream the XML from the web, it doesn't seem to to do so, but rather appears to attempt to load the entire XML file first before any parsing taking place. For modest sized XML files that's fine, but for really large ones, that's problematic.</p>
  
  <p>I have seen discussions of using <code>NSXMLParser</code> in conjunction with <a href="https://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Classes/NSXMLParser_Class/Reference/Reference.html#//apple_ref/occ/instm/NSXMLParser/initWithStream%3a" rel="nofollow"><code>initWithStream</code></a> with some customized <code>NSInputStream</code> that is streaming from the web. For example, there have been answers to this that suggest using something like the <code>CFStreamCreateBoundPair</code> referred to in the <a href="http://www.cocoabuilder.com/archive/cocoa/306924-asynchronous-downloading-and-parsing-of-xml.html" rel="nofollow">following Cocoa Builder post</a> and the discussion of <a href="https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/Streams/Articles/NetworkStreams.html#//apple_ref/doc/uid/20002277-BCIDFCDI" rel="nofollow">Setting Up Socket Streams</a> in the Apple <em>Stream Programming Guide</em>, but I have not gotten it to work. I even tried writing my own subclassed <code>NSInputStream</code> that used a <a href="http://developer.apple.com/library/ios/#documentation/Cocoa/Reference/Foundation/Classes/NSURLConnection_Class/Reference/Reference.html" rel="nofollow"><code>NSURLConnection</code></a> (which is, itself, pretty good at streaming) but I wasn't able to get it to work in conjunction with <code>NSXMLParser</code>.</p>
  
  <p>In the end, I decided to use <a href="http://www.xmlsoft.org/" rel="nofollow"><code>LibXML2</code></a> rather than <code>NSXMLParser</code>, as demonstrated in the Apple <a href="http://developer.apple.com/library/ios/#samplecode/XMLPerformance/Introduction/Intro.html" rel="nofollow">XMLPerformance sample</a>, but I was wondering if anyone had any luck getting streaming from a web source working with <code>NSXMLParser</code>. I've seen plenty of "theoretically you could do <em>x</em>" sort of answers, suggesting everything from <code>CFStreamCreateBoundPair</code> to grabbing the <a href="https://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Classes/NSURLRequest_Class/Reference/Reference.html#//apple_ref/occ/instm/NSURLRequest/HTTPBodyStream" rel="nofollow"><code>HTTPBodyStream</code></a> from <a href="https://developer.apple.com/library/ios/#documentation/Cocoa/Reference/Foundation/Classes/NSURLRequest_Class/Reference/Reference.html" rel="nofollow"><code>NSURLRequest</code></a>, but I've yet to come across a working demonstration of streaming with <code>NSXMLParser</code>.</p>
  
  <p>The Ray Wenderlich article <a href="http://www.raywenderlich.com/553/how-to-chose-the-best-xml-parser-for-your-iphone-project" rel="nofollow">How To Choose The Best XML Parser for Your iPhone Project</a> seems to confirm that <code>NSXMLParser</code> is not well suited for large XML files, but with all of the posts about possible <code>NSXMLParser</code>-based work-arounds for streaming really large XML files, I'm surprised I have yet to find a working demonstration of this. Does anyone know of a functioning <code>NSXMLParser</code> implementation that streams from the web? Clearly, I can just stick with <code>LibXML2</code> or some other equivalent XML parser, but the notion of streaming with <code>NSXMLParser</code> seems tantilizingly close.</p>