In my application the collection property is null to begin with, and I
want it to be initialized to an empty collection when the input xml does
not have the corresponding element instead of it continuing to remain
null. From what I have read so far the only way to do that is by using
post-set method. 

 

 

Other than that this is what I found, if the collection property is
initialized to say new HashSet(),  (in the constructor or in the
declaration) and the input xml does not have the corresponding element
then the following binding seems to set the property to null during
unmarshalling

 

<binding>

    <mapping name="A" class="a">

            <collection 

name="list" 

get-method="getList" 

set-method="setList"

factory="package.Factory.listFactory"  <!-this method creates and
returns an ArrayList -->

item-type="item"

usage="optional"

test-method="testListIsNullOrEmpty" />

    </mapping>

 

    <mapping ---- for item --/>

 

 

However, the following binding does not set the collection property to
null in the above situation:

 

<binding>

    <mapping name="A" class="a">

        <structure

            name="list"                   

            usage="optional"

            test-method=" testListIsNullOrEmpty">

            

            <collection

get-method="getList" 

set-method="setList"

factory="package.Factory.listFactory"  <!-this method creates and
returns an ArrayList -->

item-type="item"

 

            </structure>

    </mapping>

 

    <mapping ---- for item --/>

</binding>

 

Thanks.

 

Regards,

Sunil

________________________________

From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] On Behalf Of Sunil
Mazimdar
Sent: Thursday, March 29, 2007 2:45 PM
To: JiBX users
Subject: Re: [jibx-users] Null Collection Fields

 

Thanks for the reply. Actually the class is a pojo, with properties,
getters and setters. I tried using "field" attribute instead of
"get-method and set-method" in the collections element that did not
solve it. I still get a null reference for the collection during
unmarshalling when the element "list" is missing from the input xml.

 

<binding>

    <mapping name="A" class="a">

        <structure

            name="list"                   

            usage="optional"

            test-method=" testListIsNullOrEmpty">

            

            <collection

field="list"

factory="package.Factory.listFactory"  <!-this method creates and
returns an ArrayList -->

item-type="item"

 

            </structure>

    </mapping>

 

    <mapping ---- for item --/>

</binding>

 

Regards,

Sunil

________________________________

From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] On Behalf Of Stephan
Bourges
Sent: Thursday, March 29, 2007 1:27 PM
To: JiBX users
Subject: Re: [jibx-users] Null Collection Fields

 

>From what I could read, you will need to use post-set on your
collection.  In the post set method, you will check if the object
attribute is set to null and adjust accordingly.  Since I didn't see any
field attribute in the XML, I'm not sure if this would work since it
looks like your A class extends from ArrayList or something.  I have to
say, that I didn't need to implement any post-set in my own application,
so this is only from doc.

 

________________________________

From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] On Behalf Of Sunil
Mazimdar
Sent: Thursday, March 29, 2007 1:20 PM
To: [email protected]
Subject: Re: [jibx-users] Null Collection Fields

How to set a property reference to an empty collection instead of null
during unmarshalling when the associated element is missing from the
input xml.

 

 

This  binding causes the property to be null when the corresponding
element (list) is missing from the xml: 

 

<binding>

    <mapping name="A" class="a">

            <collection 

name="list" 

get-method="getList" 

set-method="setList"

factory="package.Factory.listFactory"  <!-this method creates and
returns an ArrayList -->

item-type="item"

usage="optional"

test-method="testListIsNullOrEmpty" />

    </mapping>

 

    <mapping ---- for item --/>

 

</binding>

 

I changed the above to the following, it did not help. 

 

<binding>

    <mapping name="A" class="a">

        <structure

            name="list"                   

            usage="optional"

            test-method=" testListIsNullOrEmpty">

            

            <collection

get-method="getList" 

set-method="setList"

factory="package.Factory.listFactory"  <!-this method creates and
returns an ArrayList -->

item-type="item"

 

            </structure>

    </mapping>

 

    <mapping ---- for item --/>

</binding>

 

 

What is the binding missing? Would using post-set method for this
purpose cause any issues/unwanted side effects?

 

Thanks.

 

Regards,

Sunil

 

-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys-and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
jibx-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/jibx-users

Reply via email to