users@jaxb.java.net

JAXB and empty elements

From: <hakon-haugfos.tangen_at_telenor.com>
Date: Fri, 14 Nov 2003 10:37:20 +0100

Hello!

I have a problem unmarshalling empty xml elements, like <SomeElement/>. I have read one of the messages in the JAXB-INTEREST archives (enclosed below in this mail), this should solve my problem, but I still get Exceptions.

The solution given in the message works fine with element with type xs:string, but not with other datatypes.

A part of my xml schema:
        <xs:element name="SomeElement" type="integer"/>


        <xs:simpleType name="integer">
                <xs:annotation>
                        <xs:appinfo>
                                <jxb:javaType name="java.lang.Integer"
                        parseMethod="com.test.MyDatatypeConverter.parseInteger"
                         printMethod="com.test.MyDatatypeConverter.printInteger"/>
                        </xs:appinfo>
                </xs:annotation>
                <xs:union memberTypes="emptyString xs:int"/>
        </xs:simpleType>
        <xs:simpleType name="emptyString">
                <xs:restriction base="xs:string">
                        <xs:enumeration value=""/>
                </xs:restriction>
        </xs:simpleType>

When I try to unmarshall a xml file with a <SomeElement/> I get a UnmarshallException:
parse event[1] severity=ERROR line#, Position (19, 14) message=Unexpected text ""
parse event[2] severity=FATAL_ERROR line#, Position (19, 14) message=Unexpected end of element {}:SomeElement") nested exception is
javax.xml.bind.UnmarshalException("Unexpected end of element {}:SomeElement")

If I change the <jxb:javaType> element to this:
                                <jxb:javaType name="java.lang.Character"
                                 parseMethod="com.test.MyDatatypeConverter.parseCharacter"
                                 printMethod="com.test.MyDatatypeConverter.printCharacter"/>

the unmarshalling works correctly.

I guess this is because "" (empty string) is a legal value for Character but not for Integer, but my code in parseInteger check for "" and return null, so I dont see why this is not allowed by the Unmarshaller.

Have anyone got an idea of how this problem can be solved, or at least why I get the Exception?

Thanks for any help!

Hakon Tangen
Telenor
Norway

-----------------------------------------------

Date: Fri, 1 Aug 2003 08:46:16 -0700
Reply-To: JAXB-INTEREST_at_JAVA.SUN.COM
Sender: Discussion list for the Java Architecture for XML Binding
              <JAXB-INTEREST_at_JAVA.SUN.COM>
From: Kohsuke Kawaguchi <Kohsuke.Kawaguchi_at_Sun.COM>
Subject: Re: Doubles and Integers
In-Reply-To: <LISTSERV%2003080108522470_at_JAVA.SUN.COM>
Content-type: text/plain; charset=US-ASCII

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
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe_at_jaxb.dev.java.net
For additional commands, e-mail: users-help_at_jaxb.dev.java.net