tags:

views:

487

answers:

1

I'm working with SOAP using the javax.xml.soap package.

I have a javax.xml.soap.SOAPMessage object that corresponds to a response to my SOAP request and I have to convert it to an instance of a class that was annotated with the javax.xml.bind.annotation.XmlType annotation.

How can I do this conversion?

+2  A: 

javax.xml.soap.SOAPMessage is a SAAJ API class. The link provides some details about the SAAJ API as well as a reference implementation. Keep in mind that the implementation in your environment may be different than the reference implementation, but you should only be concerned with the API itself and not any specifics of the implementation. Since you are talking about JAX-B 2.x annotated types, we can assume that you are dealing with SAAJ 3. Many of the SAAJ 3 classes extend DOM classes (I pretty sure this holds true in SAAJ 2, but not SAAJ 1). It just so happens that javax.xml.soap.SOAPMessage extends org.w3c.dom.Node. Conveniently, JAX-B 2.x provides an easy way to unmarshall a DOM tree (or sub-tree) into a Java type.

The following code is one way:

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Unmarshaller;

JAXBContext jc = JAXBContext.newInstance("test.jaxb");
Unmarshaller unmarshaller = jc.createUnmarshaller();
unmarshaller.unmarshal(soapBody);

Alternatively, you could turn soapBody into a JAX-P DOMSource and then unmarshall it. There are other even more roundabout ways to get from point A to point B if you are interested, but you can likely solve your problem with the code snippet above.

DavidValeri