users@jaxb.java.net

Re: Setting required and default values for attribute

From: Wolfgang Laun <wolfgang.laun_at_gmail.com>
Date: Fri, 24 Jul 2009 16:33:38 +0200

I also should have said that using a primitive type together with "optional"
is highly dangerous; to be safe the full type should be used, and here JAXB
omits use="required" but for some reason you don't want that.

What do you mean by "element not present in the XML"? If it isn't present,
it will not exit, so its defaults don't matter.

If you want to create such an element not in connection with unmarshalling,
you could use a Factory with a specific constructor call which would set the
defaults differently.

Well, you can't do it all with JAXB alone... ;-)
-W


On Fri, Jul 24, 2009 at 4:08 PM, Vladimir Blagojevic <dovlex_at_gmail.com>wrote:

> On 7/24/09 3:19 PM, Wolfgang Laun wrote:
>
>
>
> On Fri, Jul 24, 2009 at 2:38 PM, Vladimir Blagojevic <dovlex_at_gmail.com>wrote:
>
>> Hi,
>>
>> Is there a way to force schema generator not to attach use="required" for
>> every attribute of primitive type except of course to use Java primitive
>> wrappers?
>
>
> Try the annotation
> @XmlAttribute( required="false" )
>
>
> I tried that first with no luck. Is it maybe jaxb version dependent?
>
>
> How can we specify default values for attributes so they can be reflected
>> in generated schema?
>>
>
> No way. But if you go from Java to schema, your Java code would have to
> implement the default anyway.
> -W
>
> True, except I have to follow a logic that whenever element is present
> in xml it should set certain default values for attributes and when it is
> not present it should not. Any idea how to deal with this?
>
> Thanks,
> Vladimir
>
>