I have a very curious situation.
public class Child {
@XmlAttribute
public String name;
}
@XmlRootElement
public class Parent {
public static void main(final String[] args) throws Exception {
final Parent parent = new Parent();
parent.children = new ArrayList<>();
for (int i = 0; i < 3; i++) {
final Child child = new Child();
child.name = Integer.toString(i);
parent.children.add(child);
}
final JAXBContext context = JAXBContext.newInstance(Parent.class);
final Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
marshaller.marshal(parent, System.out);
}
@XmlElement(name = "child", nillable = true)
public List<Child> children;
}
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<parent>
<child name="0"/> <!-- xsi:nil expected -->
<child name="1"/>
<child name="2"/>
</parent>
Question 1: Why there is no xsi:nil attribute on those children?
xsi:nilwill only be written for items in the cle film that arenull. In your example all the items in theListare instances ofChild.Parent
If you update code in your
Parentclass to add a null into thechildrenList.Output
The
childelement corresponding to thenullentry will contain thexsi:nilattribute.