Representing element as boolean with JAXB?
- by Marcus
We have this XML:
<Summary>
<ValueA>xxx</ValueA>
<ValueB/>
</Summary>
<ValueB/> will never have any attributes or inner elements. It's a boolean type element - it exists (true) or it doesn't (false).
JAXB generated a Summary class with a String valueA member, which is good. But for ValueB, JAXB generated a ValueB inner class and a corresponding member:
@XmlElement(name = "ValueB")
protected Summary.ValueB valueB;
But what I'd like is a boolean member and no inner class:
@XmlElement(name = "ValueB")
protected boolean valueB;
How can you do this?
I'm not looking to regenerate the classes, I'd like to just make the code change manually.
Update: In line with the accepted answer, we created a new method returning the boolean value conditional on whether valueB == null.
As we are using Hibernate, we annotated valueB with @Transient and annotated the boolean getter with Hibernate's @Column annotation.