NSXMLParser Premature Document End error caused by empty nodes?
I'm using NSXMLParser to parse an XML document which is being returned from a web service hosted remotely. The XML is valid but some of the nodes will occasionally be empty, this seems to cause NSXMLParser to fall over (NSXMLParserErrorDomain error 5) however I can't find anything on the net which would suggest that there would be an issue parsing empty nodes so I wondered if anyone else had experienced any problems with this and could confirm or deny my theory that NSXMLParser can't cope with empty tags?
My XML goes something alo开发者_高级运维ng the lines of:
<Entry> <Request>2</Request> <ID>123456789</ID> <Name></Name> <UserName>username</UserName> <Password>password</Password> <SessionID>987654321</SessionID> <Oldest></Oldest> <Newest></Newest> <List></List> </Entry>
The parser throws an error when it gets to the element, it calls DidStartElement and then disappears off into the parser code and throws an error back before it calls any more delegate methods.
Your problem most likely comes from your implementation of NSXMLParser. My implementation of NSXMLParser handles empty nodes just fine, though I haven't figured out a way to actually store @"" inside of empty nodes in the dictionary that I'm producing from the XML. parser:foundCharacters: is never called when NSXMLParser hits an empty node, so your implementation there or in parser:didEndElement: is almost certainly causing the problem.
精彩评论