Flo Flo - 1 year ago 114
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.

. But the
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"

<import schemaLocation="BusinessObjectDTO.xsd"

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

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

<element name="Images">
<element name="ImageList" type="Q2:ImageType" maxOccurs="unbounded"

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download