Kumar-
First, I'm assuming the close tag on your xml is supposed to be </time>?
:) Second, looking at the java.util.Date class, I see a way to get the
UTC offset, but not a way to set it (it seems to assume the timezone of
the current computer?).
So for documentation that exists, this page:
http://castor.codehaus.org/xml-fieldhandlers.html describes one method
to perform your task.
And while I haven't coded it up, another, possibly easier, thing to do
is "trick" Castor into using fields in your class that don't actually
exist... So you can have a get/setUtcOffset method, which in the
mapping file you refer to as utcOffset, but in the get and set methods,
you simply refer to the Date object in question:
int getUtcOffset() { return time.getTimezoneOffset() }
void setUtcOffset( int offset ) { time.setTimezoneOffset( offset ) }
I don't see the latter method available in java.util.Date, but that's
the general idea. You perform a similar operation with the actual time,
but with a twist (stealing an idea from the above listed webpage):
String getTime() { return dateFormatter.format( time ) }
void setTime( String timeStr ) { time = dateFormatter.parse( timeStr ) }
So Castor only ever sees a String value... A problem might arise with
what order the data is unmarshalled in; i.e. if the timezone is set
before the time is parsed, that information is lost when the time object
is created from the parser. One work around would be to simply store
the timezone offset in the class in its own field and when the time is
parsed, set the offset based on the internal field (though I'm not sure
how Java would react to a changing timezone).
And finally, on the mapping side, it ends up looking very much like the
previous mapping file I sent you -- simply rename the fields correctly
(utcOffset, time) and remove the direct="true" (because we want Castor
to use the getters and setters).
I think that should cover it, but again, having not coded it up, I might
be overlooking something.
Stephen
Kumar Mettu wrote:
Hi,
I am trying to map xml to an existing java class. For one of the
java.util.Date field the xml is as follows:
<time utc_off="+0300">20051003152700</stop>
Is there a way to map this?
Thanks in Advance,
Kumar.
-------------------------------------------------
If you wish to unsubscribe from this list, please
send an empty message to the following address:
[EMAIL PROTECTED]
-------------------------------------------------