Question

Having no luck in generating XML with fixed element values using JAXB 2.1 RI. We're generating XML bound code using xjc and marshall the results.

It works for attributes using <xs:attribute fixed='something'/> and the JAXB customization property fixedAttributeAsConstantProperty. For elements we figured that there was no way to do the same. Or is there?

As a workaround, we used <xs:element default='something'/> which is turned into @XmlElement(default='something'). Now my guess is that you can tell the marshaller somehow to interpret the default parameter and generate an element with the corresponding content, like <element>something</element>.

I've looked at the standard and RI vendor specific marshaller configuration properties without finding something useful. There seems to be an xjc plug-in that does something similar[1], but I'd be kinda surprised if there is no standard JAXB way to do so.

Any pointers are much appreciated, thanks.

[1] http://fisheye5.cenqua.com/browse/~raw,r=1.5/jaxb2-commons/www/default-value/index.html

Was it helpful?

Solution

The XML schema specification deals with unmarshalling only, i.e. determining the post validation infoset of the source XML representation.

JAXB appears to mirror this in its support for defaults, i.e. for reading mostly. For instance, the support for default attributes does not extend to writing defaults, just to reading (or accessing from Java using the getter)

As I see it, the best bet for a solution is to extend the default value plugin to handle fixed elements as well, by making the field final and removing the setter.

The code for the plugin is very straightforward, really!

OTHER TIPS

There is an maven-plugin one can use to generate constants: https://github.com/fbdo/jaxb2-fixed-value

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top