1

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.

2
  • I do need to be able to explicitly set it, in order to override the default true value. - Can you explain more precisely? Commented Jul 10, 2013 at 17:29
  • I use attrib = null at Java level to mean "it's not specified, apply a default defined in a config file". But the default value for Element.attribute is true, so the only way to set it to null from the XML is being able to explicitly say null somehow, not to omit the attribute definition altogether, cause, I understand, this leave the default value untouched. Commented Jul 10, 2013 at 17:34

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

The absence is equivalent to 'ignore and keep the the default/previous value', so yes, apparently the only way is XmlAdapter. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.