02-23-2010 02:20 PM
I'm opening an http connection and getting (what I hope) is valid xml back. Just wondering if there's a clean way of checking "isValid" or something of the sort before I attempt to parse it. I've got it in a try/catch obviously so I have a poor man's check going on; was wondering if there's anything better.
Right now it's sort of like:
httpconnection -> inputstream -> sax.parse(is)
Is it best practice to just catch on the sax.parse rather than check before hand?
02-23-2010 02:26 PM
Maybe it's to register a handler for setErrorHandler ?
http://www.saxproject.org/apidoc/org/xml/sax/Error
02-24-2010 04:19 AM
The SAX parser reads through each byte of the input message, and, until it gets to the end, can not say for sure that the message is valid XML. As it is reading through it invokes the various call-outs (endElement for example). As long as these are coded efficiently, in most cases I would say that you would be better catching the infrequent error(s) rather than parsing it twice.
If you are getting a lot of corrupted XML, you might consider creating a mini Sax parser that does what you want. There are a number of open source SAX parsers out there, some of them really lightweight (and I suspect faster than the RIM supplied one). You could take one of these, cut out all the call backs and anything else that is overhead in your case and create a test parser.