Search code examples
javaxmljaxbwsdl2java

javax.xml.bind.UnmarshalException: unexpected element (uri:“”, local:“Managers”). Expected elements are <{}Manager>


I know there are a lot of questions like this one on SO but none provided the right answer for me so here i go.

Here is the XML i get :

<?xml version="1.0" encoding="UTF-8"?>
<Managers>
   <Manager ManagerID="1996" />
   <Manager ManagerID="1997" />
   <Manager ManagerID="1998" />
   <Manager ManagerID="1999" />
   <Manager ManagerID="2000" />
</Managers>

And here is the java class associated :

@XmlRootElement(name = "Managers")
@XmlAccessorType(XmlAccessType.FIELD)
public class ManagerList {

    @XmlElement(name = "Manager")
    private List<Manager> Managers = null;

    public List<Manager> getList() {
        return Managers;
    }

    public void setList(List<Manager> Managers) {
        this.Managers = Managers;
    }
}

And finally here is the unmarshalling part :

String stringManagers = //Here is the above XML(as String), i get it from an external source
StringReader reader = new StringReader(stringManagers);
JAXBContext context;
Unmarshaller un;

context = JAXBContext.newInstance(ManagerList.class);
un = context.createUnmarshaller();

ManagerList managerList = (ManagerList) un.unmarshal(reader);

I'm using wsdl2java to generate the java classes, and i have a package-info.java class with the namespace defined using XmlSchema annotation.

But still i'm getting the error :

javax.xml.bind.UnmarshalException: unexpected element (uri:“”, local:“Managers”). Expected elements are <{}Manager>

Any idea would be appreciated,

Thanks in advance !


Solution

  • Okay so it seems that the API i use to get my XML changed recently and even if i re-generated the classes using wsdl2java, Eclipse had a hard time understanding that.

    I just had to clean it all and start over it with fresh classes.

    Sorry for the "dumb" question !