JAXB XJC code generation - ObjectFactory class is incomplete

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

What could be the reason for this? Regards Dominik

+6
java schema xsd jaxb xjc
source share
3 answers

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

+3
source share

Do you have elements in your schema or just types? This is usually the reason.

0
source share

JAXB generates factory methods that create a JAXBElement from an object instance only if your XSD contains a complexType definition and a separate element definition using this complexType with the same name, for example:

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

In this case, JAXB will not annotate the created class with the @XmlRootElement annotation, but will provide the factory methods needed to create the JAXBElement from the object instance. This way you can easily serialize type instances without a root element as root elements.

So, you just need to add an β€œelement” declaration with the same name in addition to any complexType definition that you intend to use as a top-level element, and ObjectFactory will generate the expected factory methods.

0
source share

All Articles