users@jaxb.java.net

Re: JAXB 2.0 default values

From: Michael C. Bruton <mcbruton_at_yahoo.com>
Date: Tue, 31 Jan 2006 16:04:56 -0600

Hi.

    By the way, I wish what Joe Fialli wrote below, "If an empty element
is encountered, unmarshalling
sets the property to the default value," were true for me!

    I am using JAXB 1.6, and I defined an element like this, trying
(with no working example in the XML primer that I could find) this:

<xsd:element name="RoutingNumber" default="0">
   <xsd:simpleType>
      <xsd:restriction base="xsd:unsignedInt">
         <xsd:maxInclusive value="999999999"/>
      </xsd:restriction>
   </xsd:simpleType>
</xsd:element>

    JAXB will not plug in the default value when I run the app over the
generated classes. I get an error, "Content of element "RoutingNumber"
is incomplete"

    If anyone knows how to use default at the element (not attribute)
level with JAXB, I sure would like to know it.

    Better yet, I would like to know how to define in a schema a numeric
element (unsignedLong, unsignedInt, unsignedShort or the like) that may
either have a value or be empty. Currently I'm using <xsd:union
memberTypes="NineDigits Empty"/> to get it to happen, and in the java I
then have to test with instanceof and cast the returned object
everytime. There must be an easier way! I'm using JAXB to save time
while maintaining data integrity.

    Anyway, don't know if anyone will see this. Thanks.

    - Mike Bruton


Scott Allan wrote:

> Ok. I understand. I will try to write a plugin to do what I need.
> Thanks.
> -----Original Message-----
> *From:* Joe Fialli [mailto:Joseph.Fialli_at_Sun.COM]
> *Sent:* Tuesday, September 06, 2005 1:47 PM
> *To:* users_at_jaxb.dev.java.net
> *Subject:* Re: JAXB 2.0 default values
>
> Scott Allan wrote:
>
>>Wouldn't it also make sense to assign the default value to the
>>corresponding bean? I may not always be unmarshaling the bean to use
>>it.
>>
>>
>>
>> protected String test="testvalue";
>>
>>
>
> Scott,
>
> JAXB 2.0 is implementing element defaulting as specified by XML
> Schema specification.
> The XML Schema specification differentiates between a missing
> element and an empty element.
>
> Extracted from the XML Schema Primer: Section 2.2.1 PH 4
> (non-normative but easiest to understand.)
>
>> ... When an element is declared with a default value, the value
>> of the element is whatever value appears as the element's content
>> in the instance document; if the element appears without any
>> content, the schema processor provides the element with a value
>> equal to that of the |default
>> <http://www.w3.org/TR/xmlschema-0/#attribute-attr-default>|
>> attribute. However, if the element does not appear in the
>> instance document, the schema processor does not provide the
>> element at all. In summary, the differences between element and
>> attribute defaults can be stated as: Default attribute values
>> apply when attributes are missing, and default element values
>> apply when elements are empty.
>
> For the purposes of implementing unmarshalling, it made more sense
> that when a JAXB instance is created
> that it is assumed each of its JAXB property that maps to an XML
> element is missing. As each child XML element is
> encountered during unmarshal process, the corresponding JAXB
> property is set. If an empty element is encountered, unmarshalling
> sets the property to the default value. If a child element is
> missing, nothing is done by the unmarshal process. Thus,
> the default behavior was dictated by this fact.
>
>
> -Joe Fialli, Sun Microsystems
>
>
>>
>>
>>
>>>-----Original Message-----
>>>
>>>
>>
>>
>>
>>>From: Ed Mooney [mailto:Ed.Mooney_at_Sun.COM]
>>>
>>>
>>
>>
>>
>>>Sent: Tuesday, September 06, 2005 7:50 AM
>>>
>>>
>>
>>
>>
>>>To: users_at_jaxb.dev.java.net
>>>
>>>
>>
>>
>>
>>>Subject: Re: JAXB 2.0 default values
>>>
>>>
>>
>>
>>
>>
>>
>>
>>
>>
>>
>>>The default gets assigned when you unmarshal an element with no value:
>>>
>>>
>>
>>
>>
>>
>>
>>
>>> xjc.sh test.xsd;javac `find . -name '*.java'`;java test
>>>
>>>
>>
>>
>>
>>> parsing a schema...
>>>
>>>
>>
>>
>>
>>> compiling a schema...
>>>
>>>
>>
>>
>>
>>> testvalue
>>>
>>>
>>
>>
>>
>>
>>
>>
>>> -- Ed
>>>
>>>
>>
>>
>>
>>
>>
>>
>>>Scott Allan wrote:
>>>
>>>
>>
>>
>>
>>>>Does JAXB 2.0 support the element default attribute?
>>>>
>>>>
>>
>>
>>
>>>>for example:
>>>>
>>>>
>>
>>
>>
>>>><xs:element name="test" type="xs:string" default="testvalue"/>
>>>>
>>>>
>>
>>
>>
>>
>>
>>
>>>>This compiles as such:
>>>>
>>>>
>>
>>
>>
>>>> @XmlElement(defaultValue = "testvalue")
>>>>
>>>>
>>
>>
>>
>>>> protected String test;
>>>>
>>>>
>>
>>
>>
>>
>>
>>
>>>>but when I try to read the value, it is null:
>>>>
>>>>
>>
>>
>>
>>
>>
>>
>>>>MyObject myObject = objFactory.createMyObject();
>>>>
>>>>
>>
>>
>>
>>>>System.out.println(myObject.isSetTest());
>>>>
>>>>
>>
>>
>>
>>>>System.out.println(myObject.getTest());
>>>>
>>>>
>>
>>
>>
>>
>>
>>
>>>>false
>>>>
>>>>
>>
>>
>>
>>>>null
>>>>
>>>>
>>
>>
>>
>>
>>
>>
>>>[ ... ]
>>>
>>>
>>
>>
>>
>>
>>
>>
>>
>> _____
>>
>><< ella for Spam Control >> has removed 26707 Spam messages and set
>>aside 2 Newsletters for me
>>You can use it too - and it's FREE! www.ellaforspam.com
>>
>>
>>---------------------------------------------------------------------
>>To unsubscribe, e-mail: users-unsubscribe_at_jaxb.dev.java.net
>>For additional commands, e-mail: users-help_at_jaxb.dev.java.net
>>
>>
>>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_jaxb.dev.java.net For
> additional commands, e-mail: users-help_at_jaxb.dev.java.net
>
>
> ------------------------------------------------------------------------
> <<* ella for Spam Control *>> has removed *26722* Spam messages and
> set aside *2* Newsletters for me
> You can use it too - and it's FREE! www.ellaforspam.com
> <http://www.ellaforspam.com>
>