0

on setting an element:

element.setValue(null)

it marshalls the XML to

<element>
 <value>null<value>
</element>

I am expecting it to be:

<element>
  <value/>
</element>

or

 <element />
Sandeep Jindal
  • 14,510
  • 18
  • 83
  • 121

4 Answers4

1

JAXB does not marshal a null value as:

<foo>null</foo>

By default it will not marshal the element. If you annotate it with @XmlElement(nillable=true) the xsi:nil attribute will be used.

For More Information

bdoughan
  • 147,609
  • 23
  • 300
  • 400
0

You need to put @XmlValue annotation on getter method value

@XmlValue
public String getValue(){
}

further see How to represent null value as empty element with JAXB?

Community
  • 1
  • 1
Asif Bhutto
  • 3,916
  • 1
  • 24
  • 21
0

Try to set to element.setValue("") , instead of null directly for the desired output.

Sireesh Yarlagadda
  • 12,978
  • 3
  • 74
  • 76
0

You have two options:

  1. set the value with an empty string and do that each time you want an empty field in the file
  2. In the getter method of value you do so:

    public String getValue(){
     if(value == null)
           return null;
     return value;
    }
    

    In this case if you don't give a value to this field then you will have an empty element. Personnally, I prefer the first solution

baki
  • 95
  • 5