Tuesday, 8 July 2014

unmarshal xml to object for a nested tag



I've got some generated classes from a 3rd party provider for their API - specifically Companies House (http://ift.tt/1oDa3aN)


I'm having trouble unmarshalling the parts of the response from the API. I can cast the object to a GovTalkMessage object, which contains a Body tag - but the underlying object I get back after unsmarshalling is a ElementNSImpl object instead of the expected pojo.


Example


I create a request like this one: http://ift.tt/1xKSjhd


And get a response like this one: http://ift.tt/1xKSkBA


You can see in the reply that there is:



<Body>
<CompanyDetails xmlns="http://ift.tt/1na2M2c" xmlns:xsi="http://ift.tt/ra1lAU" xsi:schemaLocation="http://ift.tt/1xKSmcF">
...
</Body>


So the contents of the Body (which is a List<Object> in the generated GovTalkMessage pojo) should have a CompanyDetails object in the first element of the list. I instead have ElementNSImpl.


Here's the marshalling code, which works fine - can send it to the API endpoint and comes back with an xml response like the example above:



JAXBContext context = JAXBContext.newInstance(GovTalkMessage.class, CompanyDetailsRequest.class, CompanyDetails.class);
Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);

InputStream in = Request.Post ... //omitted - send to server, get response

Unmarshaller unmarshaller = context.createUnmarshaller();
GovTalkMessage reply = (GovTalkMessage)unmarshaller.unmarshal(in); //ok
Object object = reply.getBody().getAny().get(0);
//object is ElementNSImpl - should be CompanyDetails


Am I doing something wrong in the unmarshal?


Thanks in advance


No comments:

Post a Comment