Update:
When using NSXMLParser
class method initWithContentsOfURL
, 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.).
Has anyone discovered how to parse as the feed is being streamed to the device using NSXMLParser
? Yes, you can use LibXML2
(as discussed below), but it seems like it should be possible to do it with NSXMLParser
. But it's eluding me.
Original question:
I was wrestling with using NSXMLParser
to read XML from a web stream. If you use initWithContentsOfURL
, 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.
I have seen discussions of using NSXMLParser
in conjunction with initWithStream
with some customized NSInputStream
that is streaming from the web. For example, there have been answers to this that suggest using something like the CFStreamCreateBoundPair
referred to in the following Cocoa Builder post and the discussion of Setting Up Socket Streams in the Apple Stream Programming Guide, but I have not gotten it to work. I even tried writing my own subclassed NSInputStream
that used a NSURLConnection
(which is, itself, pretty good at streaming) but I wasn't able to get it to work in conjunction with NSXMLParser
.
In the end, I decided to use LibXML2
rather than NSXMLParser
, as demonstrated in the Apple XMLPerformance sample, but I was wondering if anyone had any luck getting streaming from a web source working with NSXMLParser
. I've seen plenty of "theoretically you could do x" sort of answers, suggesting everything from CFStreamCreateBoundPair
to grabbing the HTTPBodyStream
from NSURLRequest
, but I've yet to come across a working demonstration of streaming with NSXMLParser
.
The Ray Wenderlich article How To Choose The Best XML Parser for Your iPhone Project seems to confirm that NSXMLParser
is not well suited for large XML files, but with all of the posts about possible NSXMLParser
-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 NSXMLParser
implementation that streams from the web? Clearly, I can just stick with LibXML2
or some other equivalent XML parser, but the notion of streaming with NSXMLParser
seems tantilizingly close.
-[NSXMLParser initWithStream:]
is the only interface toNSXMLParser
that currently performs a streaming parse of the data. Hooking it up to an asynchronousNSURLConnection
that's providing data incrementally is unwieldy becauseNSXMLParser
takes a blocking, "pull"-based approach to reading from theNSInputStream
. That is,-[NSXMLParser parse]
does something like the following when dealing with anNSInputStream
:In order to incrementally provide data to this parser a custom
NSInputStream
subclass is needed that funnels data received by theNSURLConnectionDelegate
calls on a background queue or runloop over to the-read:maxLength:
call thatNSXMLParser
is waiting on.A proof-of-concept implementation follows: