Representing element as boolean with JAXB?

Posted by Marcus on Stack Overflow See other posts from Stack Overflow or by Marcus
Published on 2010-04-19T14:22:35Z Indexed on 2010/04/20 16:13 UTC
Read the original article Hit count: 258

Filed under:
|

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.

© Stack Overflow or respective owner

Related posts about java

Related posts about jaxb