Create java classes from xsd with jaxb from selected

I am using maven with jaxb to make classes from schema. This scheme has the following choices:

<tns:choice minOccurs="1" maxOccurs="unbounded">
  <tns:element name="video_track" type="tcore:TTrack" minOccurs="1" maxOccurs="1"/>
  <tns:element name="audio_track" type="tcore:TTrack" minOccurs="1" maxOccurs="1"/>


A change is a list with x


and / or x


. We need at least one of the audio or video tracks!

When I generate classes with this selection, I get the following code:

    @XmlElementRef(name = "audio_track", type = JAXBElement.class),
    @XmlElementRef(name = "video_track", type = JAXBElement.class)
protected List<JAXBElement<TTrack>> videoTrackOrAudioTrack;


This is not what we want. I think I am losing information because with the list TTracks

I don't know if it is video or audio.

So what am I doing wrong here?

In maven I am using org.jvnet.jaxb2.maven2 version 0.8.3


source to share

2 answers

No, you do not lose this information.

You will receive a list JAXBElement<TTrack>

. So you can check e.getName()

to see if it is an audio or video track.

You can use the Simplify plugin to get audio and video tracks in separate properties. It's not exactly what you have in your circuit, but it is pretty close and much easier to use.

This setting:

<xs:complexType name="typeWithReferencesProperty">
    <xs:choice maxOccurs="unbounded">
        <xs:element name="a" type="someType">
        <xs:element name="b" type="someType"/>



@XmlElement(name = "a")
protected List<SomeType> a;
@XmlElement(name = "b")
protected List<SomeType> b;


The reason for this pattern is maxOccurs

on yours choice


Also consider using prefixes xs:

or xsd:

for your XML schema.



I solved the problem with a simplified approach in the .xjb file:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<jaxb:bindings xmlns:jaxb=""     xmlns:xs="" xmlns:xjc="" xmlns:xsi="" xmlns:annox="" xsi:schemaLocation="" version="2.1" xmlns:simplify="" extensionBindingPrefixes="simplify">

  <jaxb:bindings schemaLocation="http://xxx/Core.xsd">
    <jaxb:bindings node="/xs:schema/xs:complexType[@name='TStreamRecorder']/xs:sequence/xs:choice/xs:element[1]">
      <simplify:as-element-property />



To activate this plugin, I changed my maven.pom file to:







This works great for me



All Articles