im working in a project. A have a Java Bean with some properties. I mark each attribute with the Tag @XmlElement, like this:
@XmlElement(name="id", nillable = true)
private String test;
<id xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
There are two ways to represent a
null value in XML.
One option is the described
xsi:nil. It just adds an empty tag for your attribute and annotates it as holding null value. This form is generated by JAXB if you annotate your attributes with
nillable = true.
Note that en empty tag is not the same as a tag with empty string - but more on that later.
Another option is not to represent that attribute at all. There will just be no tag for the attribute in the XML. JAXB uses this approach by default if you don't annotate your Java code with
nillable = true but the attributes will still have
And that's it.
Now, to what you want to achieve. Rendering
<id></id> basically means that you have an
id attribute that holds an empty string. Although you could probably find a language when this is equivalent to
null value (Oracle's PL/SQL for instance), it's obviously not an equivalent of
null value in Java.
Moreover, if the
id is modeled as
int (or any non-String type) in your Java Bean, the parser will fail to de-serialize such XML back into the bean instance.