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 />
2
  • Can you show the class of element? In particular how the value property is annotated (if at all). Commented Apr 29, 2014 at 21:09
  • value is of type xsd:String. Commented Apr 29, 2014 at 21:11

4 Answers 4

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

Sign up to request clarification or add additional context in comments.

1 Comment

This is correct. There must be some problem with my logic. JAXB does not create String "null".
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?

Comments

0

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

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.