Generate java classes from xsd with jaxb from a ch

2020-07-30 02:17发布

I use maven with jaxb to make classes out from a schema. This schema has the following choice:

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

The meening is, we want a list with x audiotracks and/or x videotracks. We need at least one of an audio or a video track!

When I generate classes with this choice 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 loose informations because with the list of TTracks I don't know if this is a video or an audio.

So what am I doing wrong here?

In maven I use org.jvnet.jaxb2.maven2 version 0.8.3

2条回答
我只想做你的唯一
2楼-- · 2020-07-30 02:50

I resolved the problem with the simplify approach in a .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 perfect for me

查看更多
叼着烟拽天下
3楼-- · 2020-07-30 03:04

No, you don't lose this information.

You get a list of JAXBElement<TTrack>. So you can check e.getName() to find out if it is an audio or a video track.

You can use my Simplify plugin to get audio and video tracks in separate properties. This is not exactly the model you have in your schema, but pretty close and much easier to use.

This customization:

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

Produces:

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

The reason you get this model is the maxOccurs on your choice.

Also consider using xs: or xsd: prefixes for your XML Schema.

查看更多
登录 后发表回答