Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JAXB marshaller - neglect integer fields that are not set

 
Anand Parthasarathy
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2198
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!
Have you tried setting minOccurs to zero or nillable to true for the fields in question?
Best wishes!
 
Anand Parthasarathy
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 68
Firefox Browser Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 656
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic