Note that there are some explanatory texts on larger screens.

plurals
  1. POnot nillable yet required and null value
    primarykey
    data
    text
    <p>I just found a curious case of JAXB with my colleague.</p> <p>I declared a field with <code>@XmlElement(required = true)</code> which is <code>nillable = false</code> as default.</p> <p>But I, by accident, tried to marshall an instance with the value of null.</p> <p>And I saw that the element is just omitted. Is it normal? Is it just a programmer's fault?</p> <pre class="lang-java prettyprint-override"><code>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; } </code></pre> <p>Here comes what I got.</p> <pre class="lang-xml prettyprint-override"><code>------------------------------------- not valid, is it? &lt;?xml version="1.0" encoding="UTF-8" standalone="yes"?&gt; &lt;doughan&gt; &lt;nill xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/&gt; &lt;/doughan&gt; -------------------------------------- valid, isn't it? &lt;?xml version="1.0" encoding="UTF-8" standalone="yes"?&gt; &lt;doughan&gt; &lt;name&gt;Blaise&lt;/name&gt; &lt;nill xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/&gt; &lt;/doughan&gt; ------------------------------------------------ schema &lt;?xml version="1.0" encoding="UTF-8" standalone="yes"?&gt; &lt;xs:schema version="1.0" xmlns:xs="http://www.w3.org/2001/XMLSchema"&gt; &lt;xs:element name="doughan" type="doughan"/&gt; &lt;xs:complexType name="doughan"&gt; &lt;xs:sequence&gt; &lt;xs:element name="name" type="xs:string"/&gt; &lt;xs:element name="nill" type="xs:string" nillable="true"/&gt; &lt;/xs:sequence&gt; &lt;/xs:complexType&gt; &lt;/xs:schema&gt; </code></pre> <p>xmllint</p> <pre><code>$ head -n100 doughan.xsd doughan1.xml doughan2.xml ==&gt; doughan.xsd &lt;== &lt;?xml version="1.0" encoding="UTF-8" standalone="yes"?&gt; &lt;xs:schema version="1.0" xmlns:xs="http://www.w3.org/2001/XMLSchema"&gt; &lt;xs:element name="doughan" type="doughan"/&gt; &lt;xs:complexType name="doughan"&gt; &lt;xs:sequence&gt; &lt;xs:element name="name" type="xs:string"/&gt; &lt;xs:element name="nill" type="xs:string" nillable="true"/&gt; &lt;/xs:sequence&gt; &lt;/xs:complexType&gt; &lt;/xs:schema&gt; ==&gt; doughan1.xml &lt;== &lt;?xml version="1.0" encoding="UTF-8" standalone="yes"?&gt; &lt;doughan&gt; &lt;nill xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/&gt; &lt;/doughan&gt; ==&gt; doughan2.xml &lt;== &lt;?xml version="1.0" encoding="UTF-8" standalone="yes"?&gt; &lt;doughan&gt; &lt;name&gt;Blaise&lt;/name&gt; &lt;nill xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/&gt; &lt;/doughan&gt; $ xmllint --schema doughan.xsd doughan1.xml &lt;?xml version="1.0" encoding="UTF-8" standalone="yes"?&gt; &lt;doughan&gt; &lt;nill xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/&gt; &lt;/doughan&gt; Element 'nill': This element is not expected. Expected is ( name ). doughan1.xml fails to validate $ xmllint --schema doughan.xsd doughan2.xml &lt;?xml version="1.0" encoding="UTF-8" standalone="yes"?&gt; &lt;doughan&gt; &lt;name&gt;Blaise&lt;/name&gt; &lt;nill xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/&gt; &lt;/doughan&gt; doughan2.xml validates $ </code></pre> <p>With respects.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload