tags:

views:

61

answers:

1

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.

+1  A: 

It's quite logic jaxb creates an inner class as it thinks that is a commplexAttribute

Instead of changing it to a boolean you could also check null == valueB if you put

@XmlElement(name = "ValueB", nillable='true') protected Summary.ValueB valueB; in your logic.

or add an extra getter that does not have @XMl.... and returns computed state of valueB perhaps what you want is possible with JAXB I have not needed it before.

Redlab
Yup, that would work. Would prefer to not have the inner class.. would be simpler.
Marcus