I’m calling a web service using JAX-WS. I want to convert the content into a Java Object.
Here is the content portion of the web service response.
<header xmlns="">
<store>
<store_id>1</store_id>
<store_name>ACME</store_name>
</store>
</header>
I then created a class as follows:
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
"store_id",
"store_name" })
@XmlRootElement(name = "store")
static public class store {
@XmlElement(name = "store_id", required = true)
protected String store_id;
@XmlElement(name = "store_name", required = true)
protected String store_name;
}
My JAXB code:
List result = service.getService1Soap12().getDivisions().getContent();
ElementNSImpl e =(ElementNSImpl)result.get(0);
JAXBContext context = JAXBContext.newInstance(store.class);
Unmarshaller um = context.createUnmarshaller();
JAXBElement element = (JAXBElement) um.unmarshal(e);
store customer = (store) element.getValue();
I get the following error:
Exception in thread "main" javax.xml.bind.UnmarshalException: unexpected element (uri:"", local:"store"). Expected elements are <{http://tempuri.org/}header>
I’ve tried countless things to fix this. Any help would be great!
Try wrapping the DOM element in an instance of
DOMSourceand doing the following:By supplying the class you wish to unmarshal to, the JAXB impl does not need to determine the unmarshal class based on the root element.