views:

27

answers:

1

Hello,

I am developing a Java application that makes an HTTP Request to a web service, and XML is returned. If the response code is 200, then a requestSucceeded() callback method will send the XML to a SAXParser with a different SAX Handler, depending on what web service is being called. If the response code is not 200, then a requestFailed() callback method is being called.

The web service that I am calling will return two types of XML documents (with a response code of 200): an XML document containing the successful response information, or an XML error document containing error information (for example, if one of the request parameters wasn't formatted correctly).

My question is this: Given my current setup, what is the best way to look for / handle both kinds of XML documents (a successful XML response or an XML error document)? The SAX Handler is looking for all of the relevant response information and it is storing that information into an object, which is then processed by my application. Is there a better solution than just always first looking for the unique XML Error tags?

Thanks!

+1  A: 

Option #1 - Change Respose Code

Why are you returning an error with response code 200? 400 (Bad Request) or another error code might be a better option. Then you could process the XML based on the response code.

Option #2 - Swap Content Handlers

Below is a link to one of my previous answers where I explain how to swap content handlers while processing the document. You could have one content handler that determines if the response is content or error, and then swaps in the appropriate content handler to process the rest.

Option #3 - Use JAXB

If the end result is that the XML will be converted to an object, have you considered using JAXB? It will build an object based on the XML based on what is returned.

Blaise Doughan
Option #2 is perfect for me, thanks! Unfortunately, I'm requesting from a web service which I did not develop, so I cannot control that there error has a response code of 200. As for option #3, I have never heard of JAXB before, but it seems really useful. I'm developing on a mobile platform which will not support it, however. I will definitely use that for something else in the future, though. Thanks so much!
behrk2