users@jaxb.java.net

RE: JAXB 2.0 default values

From: Scott Allan <sallan1000_at_aol.com>
Date: Tue, 6 Sep 2005 15:38:22 -0500

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>
<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