-2

I have a project with a working SOAP integration and I need to write some tests.

To avoid making making web service calls during tests, I saved some SOAP responses into XML files, but I don't know how to turn these XML into the appropriate objects.

I've tried using JAXBContext / Unmarshaller, but I get the error expected elements are (None), which I believe is related to the fact that the class associated with the XML response doesn't have a @XmlRootElement annotation (and I can't change this).

This question is very similar to mine, but the current answer doesn't really specify how to get the final object from the response.

It's also not clear to me if I have to leave the SOAP-specific tags or if it's easier to strip them and treat the file as a normal XML.

Schrute
  • 711
  • 7
  • 15

1 Answers1

0

After seeing this answer I got it working.

The problem was the missing @XmlRootElement as expected and in order to make it work the 2-parameter version of unmarshall() should be used as below (code from the other question):

String pathname = "file.xml";
InputStream stream = new FileInputStream(pathname); 
JAXBContext jaxbContext = JAXBContext.newInstance(UserType.class); 
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller(); 
XMLInputFactory factory = XMLInputFactory.newInstance(); 
XMLEventReader someSource = factory.createXMLEventReader(stream); 
JAXBElement<UserType> userElement = jaxbUnmarshaller.unmarshal(someSource, UserType.class); UserType user = userElement.getValue();
Schrute
  • 711
  • 7
  • 15