Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Developer
radpin
Posts: 21
Registered: ‎07-15-2008
My Device: Not Specified

using sax xml parser, what's the best way to check if the input stream "isValid" xml ?

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?

Developer
radpin
Posts: 21
Registered: ‎07-15-2008
My Device: Not Specified

Re: using sax xml parser, what's the best way to check if the input stream "isValid" xml ?

Maybe it's to register a handler for setErrorHandler ?

 

http://www.saxproject.org/apidoc/org/xml/sax/ErrorHandler.html

Developer
peter_strange
Posts: 19,610
Registered: ‎07-14-2008
My Device: Not Specified

Re: using sax xml parser, what's the best way to check if the input stream "isValid" xml ?

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