JAXB and creating elements from schema types

Working in the Java EE arena usually mean plenty of integrations – with other modules, with client applications, with third party providers, etc. Today, integrations always imply XML, and XML means some sort of definition – XML Schema, RelaxNG or “here is an example and you can figure it out yourself”. Personally I like XML schema – it’s a standard and relatively easy to understand (I know some will argue on this point). The best part is the fact I can use it to create objects for my application and save all the boilerplate marshalling and unmarshalling code. If there is one thing I hate, is to write code that should have be written by the compiler.

My tool of choice in the last few years is JAXB 2. Again, simple to understand, simple to work with, relatively descent documentation and integration with maven. However, there is one nagging problem with JAXB. When I need an element to be the root element of a document, as well as a child element I go into a corner. The trivial thing is to define a type, and then define an element of this type, in the following manner:

<?xml version="1.0" encoding="UTF-8"?>
<complexType name="SomeType">
<element name="first-name" type="string"/>
<element name="last-name" type="string"/>
<element name="some-element" type="tns:SomeType"/>

Naturally, I want to have the following code to get the simple XML:

SomeType s = new SomeType();

This should yield


Unfortunately, the generated code lacks one important annotation from the generated class – the @XmlRootElement annotation, which lets the JAXB to know that it can use this element as the root element of the XML, and let it know the name of the root element.
Apperently I’m not the only one who have encountered this problem. Once I’ve received an XML schema from a content provider whom I should have written the integration with, and the instructions were: “If you want the sample code to work, generate the classes using XJC, then go to classes X, Y and Z and add the @XmlRootElement annotation to the class”. I can’t say I was very happy with this.

However, in the current schema I design, I found a solution which is not as simple as I wanted it to be, yet it solves the problem. The definition of some-element was changed to inherit from SomeType and now it looks like this:

<complexType name="some-element">
<extension base="tns:SomeType"/>

The generated code is quite similar:

@XmlType(name = "some-element")
public class SomeElement
extends SomeType
{ }

Now, all I need to do replace the first line in the above code sample to:

SomeElement s = new SomeElement();

2 thoughts on “JAXB and creating elements from schema types

  1. Samuel Van Reeth


    I had exactly the same problem, and I found the following blogpost:

    The solution is very simple: put the following block in your schema:

    and add the following lines to the tag:

    This will force JAXB to generate the @XmlRootElement annotation

    I hope this also solves your problem

  2. Samuel Van Reeth

    A part of my previous post seems to be missing 🙁

    This is block to add in your schema:
    xjc:simple />


Leave a Reply

Your email address will not be published. Required fields are marked *