Force an element to be marshalled

View: New views
4 Messages — Rating Filter:   Alert me  

Force an element to be marshalled

by August Detlefsen :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Is there any way to force an element to be marshalled to XML even if the field in the Java object is null or empty?

I am marshalling XML to a remote web service that requires a CostCenter field in the XML, even if that field is null. Here is an example of the XML that is being produced:

<?xml version="1.0" encoding="UTF-8"?>
<Order>
<LineItem><ItemNumber>124029</ItemNumber><ItemQty>1</ItemQty><CostCenter>1234567.1234.123</CostCenter></LineItem>
<LineItem><ItemNumber>122094</ItemNumber><ItemQty>1</ItemQty></LineItem>
<LineItem><ItemNumber>119106</ItemNumber><ItemQty>1</ItemQty></LineItem>
<LineItem><ItemNumber>119105</ItemNumber><ItemQty>1</ItemQty></LineItem>
</Order>

Only the first <LineItem> includes the <CostCenter> element, but I need all of them to include it, even if it is an empty item i.e. <CostCenter></CostCenter>

__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around
http://mail.yahoo.com 

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email



Re: Force an element to be marshalled

by Werner Guttmann :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi August,

if the following is an option to you, yes there is ....

 <LineItem>
   <ItemNumber>124029</ItemNumber>
   <ItemQty>1</ItemQty>
   <CostCenter xsi:nil=true" />
</LineItem>

Werner

August Detlefsen wrote:

> Is there any way to force an element to be marshalled to XML even if the field in the Java object is null or empty?
>
> I am marshalling XML to a remote web service that requires a CostCenter field in the XML, even if that field is null. Here is an example of the XML that is being produced:
>
> <?xml version="1.0" encoding="UTF-8"?>
> <Order>
> <LineItem><ItemNumber>124029</ItemNumber><ItemQty>1</ItemQty><CostCenter>1234567.1234.123</CostCenter></LineItem>
> <LineItem><ItemNumber>122094</ItemNumber><ItemQty>1</ItemQty></LineItem>
> <LineItem><ItemNumber>119106</ItemNumber><ItemQty>1</ItemQty></LineItem>
> <LineItem><ItemNumber>119105</ItemNumber><ItemQty>1</ItemQty></LineItem>
> </Order>
>
> Only the first <LineItem> includes the <CostCenter> element, but I need all of them to include it, even if it is an empty item i.e. <CostCenter></CostCenter>
>
> __________________________________________________
> Do You Yahoo!?
> Tired of spam?  Yahoo! Mail has the best spam protection around
> http://mail.yahoo.com 
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
>     http://xircles.codehaus.org/manage_email
>
>

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email



Re: Force an element to be marshalled

by August Detlefsen :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I don't think the legacy system at the other end can handle elements like that. I actually solved the problem by forcing the value of the CostCenter field to be not-null in the Java Object. If you try to set the field to null, it uses "" instead:

    protected String costCenter = "";

    public void setCostCenter(String newValue) {
        costCenter = (newValue == null) ? "" : newValue;
    }





----- Original Message ----
From: Werner Guttmann <werner.guttmann@...>
To: dev@...
Sent: Wed, October 14, 2009 4:04:20 AM
Subject: Re: [castor-dev] Force an element to be marshalled

Hi August,

if the following is an option to you, yes there is ....

<LineItem>
   <ItemNumber>124029</ItemNumber>
   <ItemQty>1</ItemQty>
   <CostCenter xsi:nil=true" />
</LineItem>

Werner

August Detlefsen wrote:

> Is there any way to force an element to be marshalled to XML even if the field in the Java object is null or empty?
>
> I am marshalling XML to a remote web service that requires a CostCenter field in the XML, even if that field is null. Here is an example of the XML that is being produced:
>
> <?xml version="1.0" encoding="UTF-8"?>
> <Order>
> <LineItem><ItemNumber>124029</ItemNumber><ItemQty>1</ItemQty><CostCenter>1234567.1234.123</CostCenter></LineItem>
> <LineItem><ItemNumber>122094</ItemNumber><ItemQty>1</ItemQty></LineItem>
> <LineItem><ItemNumber>119106</ItemNumber><ItemQty>1</ItemQty></LineItem>
> <LineItem><ItemNumber>119105</ItemNumber><ItemQty>1</ItemQty></LineItem>
> </Order>
>
> Only the first <LineItem> includes the <CostCenter> element, but I need all of them to include it, even if it is an empty item i.e. <CostCenter></CostCenter>
>
> __________________________________________________
> Do You Yahoo!?
> Tired of spam?  Yahoo! Mail has the best spam protection around
> http://mail.yahoo.com 
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
>     http://xircles.codehaus.org/manage_email
>
>

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email

__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around
http://mail.yahoo.com 

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email



Re: Force an element to be marshalled

by Werner Guttmann-6 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi August,

remember that you can always use a FieldHandler in order not to clutter
your POPOs.

Cheers
Werner

PS I think that you could have tried to suppress output of 'xsi:'
statements as well when marshalling, but it looks like you found a solution.

August Detlefsen wrote:

> I don't think the legacy system at the other end can handle elements like that. I actually solved the problem by forcing the value of the CostCenter field to be not-null in the Java Object. If you try to set the field to null, it uses "" instead:
>
>     protected String costCenter = "";
>
>     public void setCostCenter(String newValue) {
>         costCenter = (newValue == null) ? "" : newValue;
>     }
>
>
>
>
>
> ----- Original Message ----
> From: Werner Guttmann <werner.guttmann@...>
> To: dev@...
> Sent: Wed, October 14, 2009 4:04:20 AM
> Subject: Re: [castor-dev] Force an element to be marshalled
>
> Hi August,
>
> if the following is an option to you, yes there is ....
>
> <LineItem>
>    <ItemNumber>124029</ItemNumber>
>    <ItemQty>1</ItemQty>
>    <CostCenter xsi:nil=true" />
> </LineItem>
>
> Werner
>
> August Detlefsen wrote:
>> Is there any way to force an element to be marshalled to XML even if the field in the Java object is null or empty?
>>
>> I am marshalling XML to a remote web service that requires a CostCenter field in the XML, even if that field is null. Here is an example of the XML that is being produced:
>>
>> <?xml version="1.0" encoding="UTF-8"?>
>> <Order>
>> <LineItem><ItemNumber>124029</ItemNumber><ItemQty>1</ItemQty><CostCenter>1234567.1234.123</CostCenter></LineItem>
>> <LineItem><ItemNumber>122094</ItemNumber><ItemQty>1</ItemQty></LineItem>
>> <LineItem><ItemNumber>119106</ItemNumber><ItemQty>1</ItemQty></LineItem>
>> <LineItem><ItemNumber>119105</ItemNumber><ItemQty>1</ItemQty></LineItem>
>> </Order>
>>
>> Only the first <LineItem> includes the <CostCenter> element, but I need all of them to include it, even if it is an empty item i.e. <CostCenter></CostCenter>
>>
>> __________________________________________________
>> Do You Yahoo!?
>> Tired of spam?  Yahoo! Mail has the best spam protection around
>> http://mail.yahoo.com 
>>
>> ---------------------------------------------------------------------
>> To unsubscribe from this list, please visit:
>>
>>     http://xircles.codehaus.org/manage_email
>>
>>
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
>     http://xircles.codehaus.org/manage_email
>
> __________________________________________________
> Do You Yahoo!?
> Tired of spam?  Yahoo! Mail has the best spam protection around
> http://mail.yahoo.com 
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
>     http://xircles.codehaus.org/manage_email
>
>

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email