users@jaxb.java.net

Re: How to make appear new attributes in xml file when saving them ?

From: Kohsuke Kawaguchi <Kohsuke.Kawaguchi_at_Sun.COM>
Date: Mon, 11 Jul 2005 12:26:04 -0700

Jean-Marie Condom wrote:
> Hello
>
> I use an xml file for my application where I add new attributes
> for each new version of my application ;
> those attributes are declared optional in the corresponding
> schema file with default values so that my application can read xml files
> of previous versions ;
> what i need is to make appear those new attributes
> in the xml file when i save it with my application ;
> is there way to do it ?

JAXB 2.0 handles this correctly. I don't remember how 1.0 handled
attribute defaults, but I took your e-mail to mean that it's not
printing the value.

JAXB 1.0.x retains the notion of "set"/"unset" for attributes. If the
attribute is "unset", it will still return the default value.

Perhaps you can do

        obj.setVersion(obj.getVersion());

To force the attribute to be "set"?


Alternatively you can write a SAX filter and use it with the marshaller
to post-process the generated XML. This might be too hard if you aren't
familiar with that kind of low-level XML processing.

-- 
Kohsuke Kawaguchi
Sun Microsystems                   kohsuke.kawaguchi_at_sun.com