users@jaxb.java.net

Re: Doubles and Integers

From: Kohsuke Kawaguchi <Kohsuke.Kawaguchi_at_Sun.COM>
Date: Fri, 01 Aug 2003 08:46:16 -0700

Marcus Walls <marcus.walls_at_ASPECTIVE.COM> wrote:
> However, I'd like to be able to leave the element blank and treat this in
> the same way as if the element had actually not been specified.
>
> i.e.
>
> <a></a> b is not defined
> <a><b>5</b></a> b is 5
> <a><b/></a> b is not defined - currently not supported
> <a><b></b></a> b is not defined - currently not supported


The question is more like whether you can express that constraint in W3C
XML Schema. JAXB is tied to the expressiveness of XML Schema, so if you
can't express that in XML Schema, you can't do that in JAXB.

It looks like you might be able to do that by defining:

  <element name="a">
    <simpleType>
      <union memberTypes="int">
        <simpleType>
          <restriction name="string">
            <enumeration value="" />
          </restriction>
        </simpleType>
      </
    </
  </


> I'm thinking that I may need to create my own data-type with print and
> parse methods, but I'm not sure what I'd derive from. e.g. I could create
> my own double type derived from xsd:string, but wouldn't my member
> variables then be Strings rather than double or java.lang.Double?

<javaType> customization is probably also a good choice, and you don't
need to define your own datatype for it. You can just say:

  <element name="a">
    <simpleType>
      <annotation><appinfo>
        <jaxb:javaType name="double" ... />
      </></>
      <union memberTypes="int">
        <simpleType>
          <restriction name="string">
            <enumeration value="" />
          </restriction>
        </simpleType>
      </
    </
  </

Your parseMethod and printMethod can recognize the empty string and do
the right thing.



regards,
--
Kohsuke Kawaguchi                  408-276-7063 (x17063)
Sun Microsystems                   kohsuke.kawaguchi_at_sun.com