users@jaxb.java.net

Re: diferentiating between non-existent element and element with null value

From: hotwater <nabble_at_funfab.com>
Date: Thu, 28 Feb 2008 13:41:53 -0800 (PST)

Hi, I'm not an expert, but I'll try to help. If minOccurs=0, jaxb is not
supposed to generate the element if it is null. So, try setting
minOccurs="0". What I've found is that it still does generate the element
for me. But on other web pages, people are saying that it doesn't. I think
the problem may be that in my xsd, the minOccurs=0 is set on the element
definition, but nillable="true" is set on the element's type definition,
instead of both being on the element definition.


davy123 wrote:
>
> I would like to use JAXB do differentiate between non-existent element and
> element with null value.
>
>
> I managed to do this when UNmarshalling XML-s with:
> - setting accessor type to NONE,
> - setting fields to some unique value (private static final),
> - annotating getters,
> - setting them nillable=true and
> - isSet feature checking equality to mentioned unique value with ==
> Example:
> private static final String DEFAULT_STRING = new
> String("DEFAULT_STRING");
>
> private String a = DEFAULT_STRING;
>
> @XmlElement(name = "a", nillable = true)
> public String getA() { return a; }
>
> public void setA(String value) { this.a = value; }
>
> public boolean isSetA() { return a != DEFAULT_STRING; }
>
> Unmarshalling of XML without the 'a' element causes isSetA() returning
> false, unmarshalling the 'a' with xsi:nil="true" causes isSetA() returning
> true and getA() returning null.
>
>
> The problem is the marshalling, does anyone know how to achieve
> marshalling of a property only when you want (for example when isSet
> returns true, which I have not managed to put it to work)?
>

-- 
View this message in context: http://www.nabble.com/diferentiating-between-non-existent-element-and-element-with-null-value-tp14708064p15746688.html
Sent from the java.net - jaxb users mailing list archive at Nabble.com.