aspose file tools*
The moose likes Web Services and the fly likes JAXB marshaller - neglect integer fields that are not set Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Web Services
Bookmark "JAXB marshaller - neglect integer fields that are not set" Watch "JAXB marshaller - neglect integer fields that are not set" New topic
Author

JAXB marshaller - neglect integer fields that are not set

Anand Parthasarathy
Greenhorn

Joined: May 23, 2011
Posts: 13
Hi,

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.

Regards,
-Anand
Ivan Krizsan
Ranch Hand

Joined: Oct 04, 2006
Posts: 2198
    
    1
Hi!
Have you tried setting minOccurs to zero or nillable to true for the fields in question?
Best wishes!


My free books and tutorials: http://www.slideshare.net/krizsan
Anand Parthasarathy
Greenhorn

Joined: May 23, 2011
Posts: 13
Hi Ivan,

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.

Regards,
-Anand
Philip Grove
Ranch Hand

Joined: Aug 18, 2009
Posts: 68

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.
g tsuji
Ranch Hand

Joined: Jan 18, 2011
Posts: 512
    
    3
@Anand Parthasarathy
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JAXB marshaller - neglect integer fields that are not set