users@jaxb.java.net

Re: Anonymous simple types as enums?

From: Wolfgang Laun <wolfgang.laun_at_gmail.com>
Date: Wed, 25 Feb 2009 17:13:40 +0100

Absolutely no schema changes means you'll have to use a bindings file. -
Assume a complex type on schema force.xsd where your element Product
appears:

<xsd:complexType name="DocType">
  <xsd:sequence>
    <xsd:element name="Product">
      <xsd:simpleType>
        <xsd:restriction base="xsd:string">
          <xsd:enumeration value="Product1"/>
          <xsd:enumeration value="Product2"/>
          <xsd:enumeration value="Product3"/>
        </xsd:restriction>
      </xsd:simpleType>
   </xsd:element>
    <xsd:element name="Price" type="xsd:int"/>
  </xsd:sequence>
</xsd:complexType>

Then use this binding file:

<?xml version="1.0" encoding="UTF-8"?>
<jxb:bindings version="1.0"
          xmlns:jxb="http://java.sun.com/xml/ns/jaxb"
          xmlns:xs="http://www.w3.org/2001/XMLSchema">
<jxb:bindings schemaLocation="force.xsd" node="/xs:schema">
  <jxb:bindings
node="//xs:complexType[@name='DocType']/xs:sequence/xs:element[@name='Product']/xs:simpleType">
    <jxb:typesafeEnumClass name="ProductType"/>
  </jxb:bindings>
</jxb:bindings>
</jxb:bindings>

The enum will be DocType.ProductType:

 public enum ProductType {
        PRODUCT_1("Product1"),
        PRODUCT_2("Product2"),
        PRODUCT_3("Product3");
        ...... }

-W

On Wed, Feb 25, 2009 at 3:59 PM, hstoerr <yu4cheem_at_techno.ms> wrote:

>
> When generating Java from an XSD via the XJC compiler, I always get the
> type
> java.langString for elements with anonymous simpleTypes like this:
>
> <xsd:element name="Product">
> <xsd:simpleType>
> <xsd:restriction base="xsd:string">
> <xsd:enumeration value="Product1"/>
> <xsd:enumeration value="Product2"/>
> <xsd:enumeration value="Product3"/>
> </xsd:restriction>
> </xsd:simpleType>
> </xsd:element>
>
> Of course I want like an enumeration for this. Is there a way to trick xjc
> into that? The closest I have come to is by using the customization
>
> <jxb:bindings node="//xsd:element[@name='Product']/xsd:simpleType">
> <jxb:typesafeEnumClass name="ProductType" />
> </jxb:bindings>
>
> Here, XJC actually defines an enumeration ProductType, but it is not used
> for the elements. 8-)
>
> We are using JAXB 2.1.3.
> (Before you ask: no, I cannot change the schema.)
> Thanks so much!
> --
> View this message in context:
> http://www.nabble.com/Anonymous-simple-types-as-enums--tp22204489p22204489.html
> Sent from the java.net - jaxb users mailing list archive at Nabble.com.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_jaxb.dev.java.net
> For additional commands, e-mail: users-help_at_jaxb.dev.java.net
>
>