I am using JAXB to marshall a set of Java classes into XML. But, it includes all integer/long fields which are not set with a default value of 0. I do not want them to appear when the integer/long fields are not set. Please let me know how to achieve this.
Thanks for your reply. The nillable=true works only for non-primitive data types. For e.g., if the field is of type String, by default nillable=true is enforced and the field does not appear on the XML unless it is populated. For primitive data types such as integers and booleans, the default value like '0' or 'false' appears. The @XmlElement does not have an attribute called minOccurs. Please let me know if I am wrong.
minOccurs="0" is an attribute you set in the XSD before you compile it. I do suspect that the parameter of the class exists regardless of what you do, because at the time of compilation of the XSD it is not known if the parameter will be set or not, just that it is legal not to set it.
The way to do it without tempering the existing schema is to customize the binding with a, say, jxb file. In the jxb file, use javeType element of name "java.lang.Integer" to customize the node pointing (by xpath) to the particular schema element with type xs:int. The compiler will insert further annotation to the classes and create an adapter to customize the type.
subject: JAXB marshaller - neglect integer fields that are not set