I am trying to generate a XSD file with a boolean element that is always required. When I use the XSD file to generate the java classes (with JAXB) the field is not required.
I have attempted the following versions:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:tns="http://com.my.example/test"
targetNamespace="http://com.my.example/test"
elementFormDefault="qualified">
<xs:complexType name="MyComplexElement">
<xs:sequence>
<xs:element name="MySimpleElement" type="xs:boolean" minOccurs="1" maxOccurs="1" nillable="false"/>
<xs:element name="MySimpleElement2" type="xs:boolean" minOccurs="1" maxOccurs="1" nillable="true"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
That XSD file generates me the following fields in the Java class:
@XmlElement(name = "MySimpleElement")
protected boolean mySimpleElement;
@XmlElement(name = "MySimpleElement2", required = true, type = Boolean.class, nillable = true)
protected Boolean mySimpleElement2;
Whereas I was expecting something like this:
@XmlElement(name = "MySimpleElement", required=true) // This one would be my preferred one
protected boolean mySimpleElement;
@XmlElement(name = "MySimpleElement2", required = true, type = Boolean.class, nillable = false)
protected Boolean mySimpleElement2;
I also have attempted to generate the XSD file from a java class but I wasn't able to do it either.
Note: I am using org.codehaus.mojo.jaxb2-maven-plugin version:2.5.0
An xml element of type xs:boolean
which is not nillable can be represented in java by primitive data type boolean
because a boolean
always has a value (true
or false
) - it cannot be null
. Because it always has a value, it is not needed to specifically mark it as required to have the xml element generated.
An xml element of type xs:boolean
which is nillable cannot be represented by primitive data type boolean
because a boolean
cannot be null. Therefore it is represented by a Boolean
object. Because it can be null, it is needed to specifically mark it as required to have the xml element generated even if it is null
.