Question

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

Was it helpful?

Solution

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.

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top