Flo Flo - 1 month ago 4
Java Question

JAXB doesn't generate all element classes

For a REST service I define the necessary DTOs in XML to generate JAXB object of it. For generation I use the built in option of the Eclipse IDE.

The problem is that the classes don't get generated as expected. Given the XML Schema file below I'm expecting 3 classed to be generated.

ImageType
,
Image
and
Images
. But the
Image
class for the Image element doesn't get generated. At the moment I don't know what I'm doing wrong.

<?xml version="1.0" encoding="UTF-8"?>
<schema targetNamespace="http://www.company.com/schema/v1/ImageDTO"
elementFormDefault="qualified" xmlns="http://www.w3.org/2001/XMLSchema"
xmlns:Q1="http://www.company.com/schema/v1/BusinessObjectDTO"
xmlns:Q2="http://www.company.com/schema/v1/ImageDTO">

<import schemaLocation="BusinessObjectDTO.xsd"
namespace="http://www.company.com/schema/v1/BusinessObjectDTO"></import>

<complexType name="ImageType" abstract="false">
<complexContent>
<extension base="Q1:BusinessObjectType">
<sequence>
<element name="name" type="string" maxOccurs="1" minOccurs="1"></element>
<element name="fileName" type="string" maxOccurs="1"
minOccurs="1"></element>
<element name="thumbnailFileName" type="string" maxOccurs="1"
minOccurs="1"></element>
</sequence>
</extension>
</complexContent>
</complexType>

<element name="Image" type="Q2:ImageType"></element>

<element name="Images">
<complexType>
<sequence>
<element name="ImageList" type="Q2:ImageType" maxOccurs="unbounded"
minOccurs="0"></element>
</sequence>
</complexType>
</element>
</schema>

G_H G_H
Answer

You've defined Image as an element of type ImageType. Image is thus just a name used with that type. The ImageType definition will be turned into a Java class, and when the Image element is refered somewhere in your schema, that'd result in a field of type ImageType annotated as being an XML element with the name Image.

So say that you have...

<element ref="Image" minOccurs="1" maxOccurs="1" />

somewhere in a type definition, that'd result in ...

@XmlElement(name="Image" ...)
ImageType image;

... in the corresponding class.

The reason why Images did get a class definition is because you've defined it as a complexType inline. Image refers to a type, so they're just using the corresponding class. Images has an anonymous type definition, so a class must be generated to capture its structure.