not nillable yet required and null value

jaxb

Solution

Yes, if an element is not annotated with `@XmlElement(nillable=true)` then null values will be omitted from the XML document.

For More Informatation

- http://blog.bdoughan.com/2012/04/binding-to-json-xml-handling-null.html

Note: I'm guessing that you were targetting me with that question :).

Problem

I just found a curious case of JAXB with my colleague. I declared a field with `@XmlElement(required = true)` which is `nillable = false` as default. But I, by accident, tried to marshall an instance with the value of null. And I saw that the element is just omitted. Is it normal? Is it just a programmer's fault? ``` import java.io.*; import javax.xml.bind.*; import javax.xml.bind.annotation.*; import javax.xml.transform.*; import javax.xml.transform.stream.*; @XmlAccessorType(XmlAccessType.NONE) @XmlRootElement public class Doughan { public static void main(final String[] args) throws JAXBException, IOException { final JAXBContext context = JAXBContext.newInstance(Doughan.class); final Marshaller marshaller = context.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); System.out.println("\n------------------------------------- not valid, is it?"); marshaller.marshal(new Doughan(), System.out); System.out.println("\n------------------------------------------------- valid"); marshaller.marshal(new Doughan("Blaise"), System.out); System.out.println("\n------------------------------------------------ schema"); context.generateSchema(new SchemaOutputResolver(){ @Override public Result createOutput(final String namespaceUri, final String suggestedFileName) throws IOException { return new StreamResult(System.out) { @Override public String getSystemId() { return "Do I really have to do this?"; } }; } }); } public Doughan() { this(null); } public Doughan(final String name) { super(); this.name = name; } @XmlElement(required = true) // default; nillable = false private String name; @XmlElement(nillable = true, required = true) private String nill = null; } ``` Here comes what I got. ``` ------------------------------------- not valid, is it? <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <doughan> <nill xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/> </doughan> -------------------------------------- valid, isn't it? <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <doughan> <name>Blaise</name> <nill xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/> </doughan> ------------------------------------------------ schema <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <xs:schema version="1.0" xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:element name="doughan" type="doughan"/> <xs:complexType name="doughan"> <xs:sequence> <xs:element name="name" type="xs:string"/> <xs:element name="nill" type="xs:string" nillable="true"/> </xs:sequence> </xs:complexType> </xs:schema> ``` xmllint ``` $ head -n100 doughan.xsd doughan1.xml doughan2.xml ==> doughan.xsd <== <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <xs:schema version="1.0" xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:element name="doughan" type="doughan"/> <xs:complexType name="doughan"> <xs:sequence> <xs:element name="name" type="xs:string"/> <xs:element name="nill" type="xs:string" nillable="true"/> </xs:sequence> </xs:complexType> </xs:schema> ==> doughan1.xml <== <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <doughan> <nill xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/> </doughan> ==> doughan2.xml <== <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <doughan> <name>Blaise</name> <nill xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/> </doughan> $ xmllint --schema doughan.xsd doughan1.xml <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <doughan> <nill xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/> </doughan> Element 'nill': This element is not expected. Expected is ( name ). doughan1.xml fails to validate $ xmllint --schema doughan.xsd doughan2.xml <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <doughan> <name>Blaise</name> <nill xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/> </doughan> doughan2.xml validates $ ``` With respects.

Original source