Monday, June 24, 2013

Jaxb not adding the @XMLRoot element.

There are those cases where you are trying to build an object Model from XSD and sees that there is no @XMLRootElement tag added to the root element in the stub and your unmarshelling starts failing. The  basic idea is that if we can statistically guarantee that a complex type won't be used by multiple different tag names, XJC put @XmlRootElement. 

Try for the basic schema here. You see the root element gets generated.

The issue happens when you have something like this

 <element name="Employee" type="inc:CTEmployee" />
 <complexType name="CTEmployee" />

You see the Employee element is not tagged with the @XMlRootElement in the stub. The Assumption that XJC does is that "your schema might be used by other schema's that XJC isn't compiling right now".

So basic fix is avoid doing the Complex type defined outside on same name for the root element and it will work fine or avoid the ambugity by removing multiple references like the ones shown above.

No comments:

Post a Comment