Skip to main content

Can I unmarshal into 2 different POJOs??

Posted by kam5FCC on September 9, 2013 at 1:52 PM PDT

I need to get REST a response from a web service that will send different XML data depending on the status of the response. For instance, when trying to log in I could get
< loginResponse >
< result > Succeed < /result >
< id > 3874231209809832049328757905-093-2103921-4839324302943 < /id >
< /loginResponse >
or
< loginResponse >
< result > Fail < /result >
< error >
< msg > Bad User ID < /msg >
< code > 9856 < /code >
< /error >
< /loginResponse >

I am presently using this code
JAXBContext jc = JAXBContext.newInstance(LoginResponse.class);
InputStream xml = connection.getInputStream();
LoginResponse response = (LoginResponse)jc.createUnmarshaller().unmarshal(xml);

But this isn't working since I don't know if the login passed or failed. Therefore I don't know which way to cast the unmarshalled file. Is there a way to examine the results before unmarshalling so I can direct which POJO to use? Or is there a better way?

Thanks,
Kevin