XJC Java class generation for <xs:choice> element which is not unbounded

懵懂的女人 提交于 2019-12-06 14:12:41

You can using the following XJC binding to achieve this.

<xs:complexType name="myClass">
  <xs:sequence>
    <xs:choice>
      <xs:annotation>
        <xs:appinfo>
          <jaxb:property name="bookOrJournal"/>
        </xs:appinfo>
      </xs:annotation>
      <xs:element name="Book" type="Book"/>
      <xs:element name="Journal" type="Journal"/>
    </xs:choice>
  </xs:sequence>
</xs:complexType>

After executing xjc <XSD File> -extension, this generated the following Java class for me.

@XmlElements({
    @XmlElement(name = "Book", type = Book.class),
    @XmlElement(name = "Journal", type = Journal.class)
})
protected Publication bookOrJournal;

To use the XJC binding, I added the following to the top of my XSD.

<xs:schema version="1.0" xmlns:xs="http://www.w3.org/2001/XMLSchema"
  xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
  xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
  jaxb:version="1.0" jaxb:extensionBindingPrefixes="xjc">
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!