Reputation: 73
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>
Upvotes: 7
Views: 10595
Reputation: 29703
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>
Upvotes: 4