[javax.xml.bind.UnmarshalException: unexpected element

cxf, jaxb, mule

Solution

You need to leverage the package level `@XmlSchema` annotation (on a specical class called `package-info` to specify the namespace qualification. If you already have a package-info.java file make sure it is being compiled.

package-info.java

Below is the complete contents of the `package-info.java` file. You will need to change the package from `example` to the package that contains your domain model to which you want the namespace qualification applied.

@XmlSchema(
    namespace = "urn:partner.soap.sforce.com",
    elementFormDefault = XmlNsForm.QUALIFIED)
package example;

import javax.xml.bind.annotation.*;

For More Information

You can find out more information about JAXB and namespace qualification on my blog:

- http://blog.bdoughan.com/2010/08/jaxb-namespaces.html

Problem

When invkoing a webservice, I get the following exception: [javax.xml.bind.UnmarshalException: unexpected element (uri:"urn:partner.soap.sforce.com", local:"metadataServerUrl"). Expected elements are <{}sessionId>,<{}sandbox>,<{}userId>,<{}passwordExpired>,<{}metadataServerUrl>,<{}userInfo>,<{}serverUrl>] The response expected is actually an object called LoginResult. But however I see the element names in the exception strace. The loginResult class is the expected output object from the webservice call. Please suggest how to fix this. ``` @XmlAccessorType(XmlAccessType.FIELD) @XmlRootElement(name = "result") ``` public class LoginResult { ``` @XmlElement(name = "metadataServerUrl") protected String metadataServerUrl; @XmlElement(name = "passwordExpired") protected boolean passwordExpired; @XmlElement(name = "sandbox") protected boolean sandbox; @XmlElement(name = "serverUrl") protected String serverUrl; @XmlElement(name = "sessionId") protected String sessionId;` @XmlElement(name = "userId") protected String userId; @XmlElement(name = "userInfo") protected GetUserInfoResult userInfo; ```

Original source