cubesoft
cubesoft

Reputation: 3550

JAXB XJC code generation - ObjectFactory class is incomplete

I generate Java classes from my XSD schema file using XJC command line tool. The ObjectFactory class generates incomplete content. It generates creation methods without JAXBElement<Type> createType decoration.

What may be the reason of this? Regards Dominik

Upvotes: 6

Views: 4858

Answers (2)

Mirko Klemm
Mirko Klemm

Reputation: 2068

JAXB generates factory methods that create a JAXBElement from an object instance only if your XSD contains both a complexType definition and a separate element definition using that complexType WITH THE SAME NAME, for example:

<complexType name="my-type">
   ...
</complexType>

<element name="my-type" type="tns:my-type"/>

In this case, JAXB won't annotate the generated class with an @XmlRootElement annotation, but will provide the factory methods you need to create a JAXBElement from the object instance. That way, you can serialize instances of non-root-element types as root elements easily.

So, you should just add an "element"-declaration with the same name in addition to any complexType definition you intend to be used as a top-level element, and ObjectFactory will generate the expected factory methods.

Upvotes: 0

skaffman
skaffman

Reputation: 403481

Only some types in a JAXB2 XJC-generated binding need JAXBElement wrappers. Those types that have the @XMLRootElement annotation do not need the wrapper, and so the object factory does not generate one.

Upvotes: 4

Related Questions