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"/>
</tns:choice>

      

A change is a list with x

audiotracks

and / or x

videotracks

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

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

@XmlElementRefs({
    @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

+1


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:annotation>
                <xs:appinfo>
                    <simplify:as-element-property/>
                </xs:appinfo>
            </xs:annotation>
        </xs:element>
        <xs:element name="b" type="someType"/>
    </xs:choice>
</xs:complexType>

      



Outputs:

@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.

+4


source


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="http://java.sun.com/xml/ns/jaxb"     xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:annox="http://annox.dev.java.net" xsi:schemaLocation="http://java.sun.com/xml/ns/jaxb http://java.sun.com/xml/ns/jaxb/bindingschema_2_0.xsd" version="2.1" xmlns:simplify="http://jaxb2-commons.dev.java.net/basic/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 />
    </jaxb:bindings>
  </jaxb:bindings>

</jaxb:bindings>

      

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



  <plugin>
    <groupId>org.jvnet.jaxb2.maven2</groupId>
    <artifactId>maven-jaxb2-plugin</artifactId>
    <executions>
      <execution>
        <id>process-xsd</id>
        <goals>
          <goal>generate</goal>
        </goals>

        <phase>generate-sources</phase>
        <configuration>
          <args>
            <arg>-Xsimplify</arg>
            <arg>-Xannotate</arg>                
          </args>

          <schemas>
            <schema>
              <url>http://xxxConfiguration.xsd</url>
            </schema>
          </schemas>

          <bindingIncludes>
            <include>schema/configBinding.xjb</include>
            <include>schema/coreBinding.xjb</include>
          </bindingIncludes>


        </configuration>
      </execution>
    </executions>
  </plugin>

      

This works great for me

+2


source







All Articles