ShiwamP
ShiwamP

Reputation: 5

XSD conversion to Java is different for elements with minOccurs="0" than for elements with default occurence

Given 2 elements a and b in an XSD file. Both are of type int but the difference is that a is having minOccurs="0" while b is not. So now when a Java class is generated out of this XSD, it contains a of type Integer while b is of type int. Please explain.

<?xml version="1.0" ?>
<xs:complexType name="SearchAB">
<xs:element name="a" minOccurs="0" type="xs:int">
<xs:annotation>`enter code here`
    <xs:documentation>a</xs:documentation>
</xs:annotation>
</xs:element>
<xs:element name="b" type="xs:int">
<xs:annotation>
    <xs:documentation>b</xs:documentation>
</xs:annotation>
</xs:element>"
</xs:complexType>

Java class:

protected Integer a;
protected int b;

Upvotes: 0

Views: 539

Answers (1)

Meyer
Meyer

Reputation: 1712

Case b
By default, minOccurs="1" and maxOccurs="1". Therefore, there is exactly one integer element (not fewer and not more), and it can be efficiently mapped to a simple int.

Case a
If an element has set minOccurs="0", it is optional. However, in Java, an int cannot be null. So what value would you use to represent that the element is omitted? To deal with this possibility, the value is therefore stored as an Integer object, which can be null. So, if the element is present, the a variable will have an integer value. Otherwise, it is set to null to represent the omitted element.

Upvotes: 1

Related Questions