Discussion:
[castor-dev] Force an element to be marshalled
August Detlefsen
2009-10-13 19:00:24 UTC
Permalink
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
Werner Guttmann
2009-10-14 11:04:20 UTC
Permalink
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
Post by August Detlefsen
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?
<?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
---------------------------------------------------------------------
http://xircles.codehaus.org/manage_email
---------------------------------------------------------------------
To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email
August Detlefsen
2009-10-15 20:05:32 UTC
Permalink
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 <***@gmx.net>
To: ***@castor.codehaus.org
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
Post by August Detlefsen
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?
<?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
---------------------------------------------------------------------
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
Werner Guttmann
2009-10-16 08:12:43 UTC
Permalink
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.
Post by August Detlefsen
protected String costCenter = "";
public void setCostCenter(String newValue) {
costCenter = (newValue == null) ? "" : newValue;
}
----- Original Message ----
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
Post by August Detlefsen
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?
<?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
---------------------------------------------------------------------
http://xircles.codehaus.org/manage_email
---------------------------------------------------------------------
http://xircles.codehaus.org/manage_email
__________________________________________________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com
---------------------------------------------------------------------
http://xircles.codehaus.org/manage_email
---------------------------------------------------------------------
To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email

Loading...