Question

I have some XML that is mapped to a Java class and contains an attribute like

<element attrib = 'true|false|null'>

which means, that attribute is mapped to a Boolean field and can be true/false or null. I do need to be able to explicitly set it, in order to override the default true value.

Is this possible in XML? And is it possibile in JAXB? If not, is there a best practice to recommend? I've read around that you can do that using a sub-element, however I don't like that, when not needed. I'd like to keep attrib as attrib. For the moment, it sounds like the only way is to use an XmlAdapter, I wonder if there is a quicker way.

Was it helpful?

Solution

You would need to use an XmlAdapter for this use case. If you can represent null as the absence of that attribute then you could just make the field/property of type Boolean.

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