Parsing Multiple XML Docs from a Continuous Stream with NSXMLParser
My iPhone App is socket based and receives a continuous, non-delimited stream of XML documents one after the other in which I intend to parse with the event-based NSXMLParser.
Example: 2 documents one after the other
<?xml version='1.0' encoding='UTF-8' ?><document name="something"><foo>bar</foo></document><?xml version='1.0' encoding='UTF-8'开发者_如何学Python ?><document name="somethingelse"><bar>foo</bar></document>
In the Java-based implementation of this, the XML parser simply parses a stream until it reaches the end of the document, at which point it does its thing and then starts parsing the next document from that point in the stream.
The problem is NSXMLParser does not accept a stream and does not tell me at what point in the NSData it finished parsing (except for a useless line and column number).
I have seen some solutions like the AQ StreamingXMLParser but again, when this gets to the end of the document it just stops and wont attempt to parse another document, or tell me exactly where in the stream it finished so that I can start a new parse.
You will likely have to drop down to libxml using the push parser context. Take a look at the sample project XMLPerformance which implements this in LibXMLParser. http://developer.apple.com/iphone/library/iPad/index.html#samplecode/XMLPerformance/Listings/Classes_LibXMLParser_m.html%23//apple_ref/doc/uid/DTS40008094-Classes_LibXMLParser_m-DontLinkElementID_10
精彩评论