Using JAXB is it possible to ensure that null values are not marshalled as () empty elements. For instance
public class Contacts {
@XmlElement(name = "Phone")
protected List<Phone> phone;
}
currently if one of the phone element is null i get
<contact>
</phone>
<phone>
<areacode>919</areacode>
<phonenumber>6785432</phonenumber>
</phone>
</contact>
i want the following output
<contact>
<phone>
<areacode>919</areacode>
<phonenumber>6785432</phonenumber>
</phone>
</contact>
Null values are not marshaled as empty element by default.
Only empty values are marshalled as empty element
In your example you are using collection with empty Phone object
element. You have two elements in list: empty Phone
(all fields are null
) and Phone object
with not null fields.
So,
public class Contacts {
@XmlElement(name = "Phone")
protected List<Phone> phone = Arrays.asList(new Phone[]{null, null, null});
}
will be marshalled to
<contact/>
but
public class Contacts {
@XmlElement(name = "Phone")
protected List<Phone> phone = Arrays.asList(new Phone[]{new Phone(), new Phone(), null});
}
will be marshalled to
<contact>
<Phone/>
<Phone/>
</contact>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With