I have a Object with two fields “name” and “address”. JAXB ignores the empty elements while transforming the object into XMl.
For ex: if I have name=”xyz” and address=null then out will be
<name>xyz</name>
but what I want as an output as
<name>xyz</name>
<address></address>
I have seen the option @XmlElement(nillable="true") but this gives the output as
<name>xyz</name>
<address xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
Please help me getting the desired output.
Thanks in advance.
A JAXB (JSR-222) implementation will output an empty String
""value as an empty element. You can set theaddressproperty to this to get the desired effect.UPDATE #1
You could leverage Marshal Event Callbacks to adjust the value of
address.UPDATE #2
If you use EclipseLink MOXy as your JAXB provider (I’m the MOXy lead), then you could use an
XmlAdapterfor this use case.XmlAdapter (StringAdapter)
package-info
Then if you specify it at the package level it will apply to all mapped fields/properties of type
Stringwithin that package.For More Information