JAXB will marshal an empty string (""
) as an empty element. You could change your code so that when the field has a value of (""
) the property reports a null value.
@XmlRootElement(name = "data")
static class Data
{
private String orderDetail = "";
@XmlElement (name = "order-detail", nillable = true)
public String getOrderDetail() {
if(orderDetail.length() == 0) {
return null;
}
return orderDetail;
}
public void setOrderDetail(String orderDetail) {
if(null == orderDetail) {
this.orderDetail = "";
} else {
this.orderDetail = orderDetail;
}
}
}
Normally an XmlAdapter
is used to "fix up" values, but the JAXB reference implementation does not apply an XmlAdapter
to null
values.
Note
In short: I want to properly depict members that are null in the resulting xml file.
Any empty element is not a valid representation of null
in XML.