Jibx + xsi:nil=”true”

Jibx is nice framework for marshaling (java object to xml) and un-marshaling (xml to java obj).

In case of marshaling, if an element is null, Jibx does not populate that element in generated xml.  If it required element as per XSD it throws error indicating that require field is absent in java object.

So if you want to  populated xsi:nil=”true”  in case an element is null. There is not easy work around.

There is crude hack, where you define in the binding that said element is required (by not specifying usage=”optional”).  It may not be required element in XSD but that’s OK. This just to force Jibx not to ignore the element in response. And also mark element as nillable=”true”. 

<binding name=”sample_with_long” package=”com.maventechnologies” trim-whitespace=”true”>
<namespace uri=”http://www.maventechnologies.com&#8221; default=”elements” prefix=”tns”/>
<mapping class=”com.maventechnologies.Account” name=”Account”>
<value style=”element” name=”AccountID” get-method=”getAccountID” set-method=”setAccountID” nillable=”true”/> <!– usage=”optional” is not present so jibx think it required element –>
</mapping>
</binding>

So this causes Jibx to think this element is require and nillable so if value is null in java object it will populate the element as below. Sample request generated is 

<?xml version=”1.0″ encoding=”UTF-8″ standalone=”yes”?>
<tns:Account xmlns:tns=”http://www.maventechnologies.com&#8221;
xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”&gt;
<tns:AccountID xsi:nil=”true” />
</tns:Account>

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s