users@jaxb.java.net

Re: Marshalling Dates

From: Charles Overbeck <charles_overbeck_at_yahoo.com>
Date: Mon, 15 Mar 2010 13:24:43 -0700 (PDT)

Hi Wolfgang,

I'm trying to strip out the timezone information when spitting out the XML. Right now, my XML snippet looks like this:

<activeSince>2008-05-16-07:00</activeSince>

I want it to be:

<activeSince>2008-05-16</activeSince>

But I'm OK with it continuing to be an XMLGregorianCalendar in the app. And want to to be able to read the timezone if it submitted. It's a little strange that way, I'll admit :).

Since I posted my initial message, I've been reading more and just discovered how you can customize binding declarations for XSD compilation (I'm relatively new to JAXB). It's looking to me like that might be the way to go. Does that sound right?

Thanks,

Charles




________________________________
From: Wolfgang Laun <wolfgang.laun_at_gmail.com>
To: users_at_jaxb.dev.java.net
Sent: Mon, March 15, 2010 1:11:03 PM
Subject: Re: Marshalling Dates

What do you want to do? Jennie Hall's article describes a mapping by adapter between String in the XML and java.util.Date in the application. What do you want on these two sides?
-W




On Mon, Mar 15, 2010 at 6:52 PM, Charles Overbeck <charles_overbeck_at_yahoo.com> wrote:

>Hello,
>
>>I'm trying to customize the mapping of dates/XMLGregorianCalendar during marshalling, simliar to what is described in this article:
>
>http://blogs.sun.com/CoreJavaTechTips/entry/exchanging_data_with_xml_and
>
>>Look for the text "Although JAXB can handle marshalling java.util.Date to XML"... for the relevant part of the article.
>
>>The reason I can't follow the steps in that article exactly is that I am generating the JAXB beans from XML/.XSD, rather than having the XML generated from Java Beans. So I can't add annotations to the Java Beans as the article describes.
>
>>I thought I could explicitly set an XML adapter to handle this on the marshaller, but I can't get it working. I hope and expect I am missing something obvious. Here is what I am basically doing:
>
>>- I am using JAXB 2.1.10 (with Jersey if that matters, although I don't think it should).
>
>>- My .XSD contains a line like this:
>
>> <xsd:element name="activeSince" type="xsd:date" maxOccurs="1" minOccurs="0"></xsd:element>
>
>>The generated bean then looks like this:
>
>> @XmlSchemaType(name = "date")
>> protected XMLGregorianCalendar activeSince;
>
>>I create a Marshaller via the JAXBContext and try this (I know the marshaller is being used because I get errors if there are schema errors).
>
>> marshaller.setSchema(mySchema);
>> marshaller.setAdapter(new DateAdapter());
>
>>And my DateAdapter looks like this:
>
>> private static class DateAdapter extends XmlAdapter<String, XMLGregorianCalendar> {
>> @Override
>> public String marshal(XMLGregorianCalendar v) throws Exception {
>> return "hello"; // This will not be the real code obviously -- just a test to see my adapter is being invoked
>> }
>> @Override
>> public XMLGregorianCalendar unmarshal(String v) throws Exception {
>> return null; // I don't care about unmarshalling for this test
>> }
>> }
>
>>What happens is that the default date marshalling occurs.
>
>>Any help is appreciated.
>
>>Thanks,
>
>>Charles
>
>>---------------------------------------------------------------------
>>To unsubscribe, e-mail: users-unsubscribe_at_jaxb.dev.java.net
>>For additional commands, e-mail: users-help_at_jaxb.dev.java.net
>
>