سؤال

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 />
هل كانت مفيدة؟

المحلول

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

نصائح أخرى

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?

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

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

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top