I have a XSD like the following:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" version="1.0">
<xs:element name="foo">
<xs:complexType>
<xs:sequence>
<xs:element name="bar">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute name="type" use="required">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="a"/>
<xs:enumeration value="b"/>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
<xs:attribute name="subtype" use="required">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="aa"/>
<xs:enumeration value="bb"/>
<xs:enumeration value="cc"/>
<xs:enumeration value="dd"/>
<xs:enumeration value="ee"/>
<xs:enumeration value="ff"/>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
<xs:element name="something">
<xs:complexType>
<xs:attribute name="name" type="xs:string" use="required"/>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
but JAXB does not generate enums:
@XmlAttribute(name = "type", required = true)
protected String type;
@XmlAttribute(name = "subtype", required = true)
protected String subtype;
I'm using the maven-jaxb2-plugin
to generate the JAXB classes.
How can I generate enums in this case? Why doesn't it work out-of-the-box?
Please note that I cannot change the XML structure. I'd prefer a custom binding solution (using a xjb file), but I might be able to change the XSD as well, as long as it doesn't change the XML structure.
Thanks to https://stackoverflow.com/a/666722/506855 I managed to define the following bindings file, which did the trick:
<?xml version="1.0" encoding="UTF-8"?>
<jxb:bindings xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jxb="http://java.sun.com/xml/ns/jaxb"
version="2.1">
<jxb:bindings schemaLocation="testError.xsd">
<jxb:bindings node="//xs:attribute[@name='type']/xs:simpleType">
<jxb:typesafeEnumClass name="Type" />
</jxb:bindings>
<jxb:bindings node="//xs:attribute[@name='subtype']/xs:simpleType">
<jxb:typesafeEnumClass name="Subtype" />
</jxb:bindings>
</jxb:bindings>
</jxb:bindings>
There is also a related (and old) JAXB issue: https://java.net/jira/browse/JAXB-318
Your enum is an inner simpleType
. Change your xsd in
<xs:element name="foo">
<xs:complexType>
<xs:sequence>
<xs:element name="bar">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute name="type" use="required">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="a" />
<xs:enumeration value="b" />
</xs:restriction>
</xs:simpleType>
</xs:attribute>
<xs:attribute name="subtype" use="required" type="CodeEnum"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
<xs:element name="something">
<xs:complexType>
<xs:attribute name="name" type="xs:string" use="required" />
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:simpleType name="CodeEnum">
<xs:restriction base="xs:string">
<xs:enumeration value="aa" />
<xs:enumeration value="bb" />
<xs:enumeration value="cc" />
<xs:enumeration value="dd" />
<xs:enumeration value="ee" />
<xs:enumeration value="ff" />
</xs:restriction>
</xs:simpleType>
Note: For this solution the xml will be the same.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With