Hi Wolfgang,
Thanks! That helped in fill the missing areas and I've now got it doing what I want it to do.
Charles
________________________________
From: Wolfgang Laun <wolfgang.laun_at_gmail.com>
To: users_at_jaxb.dev.java.net
Sent: Mon, March 15, 2010 1:38:06 PM
Subject: Re: Marshalling Dates
It so happens that I have this lying around on my disk, which investigates several variants of doing date/time. The binding can be done with a separate file if you don't want to put it in your XML schema. If you want to have date+TZ, you'll have to modify the adapter, but that's easy.
xmlns:jaxb="
http://java.sun.com/xml/ns/jaxb"
jaxb:version="2.1" version="2.0">
<xs:element name="doc" type="DocType"/>
<xs:simpleType name='DateWithoutTimezoneInfo'>
<xs:restriction base='xs:date'/>
</xs:simpleType>
<xs:annotation>
<xs:appinfo>
<jaxb:globalBindings>
<jaxb:javaType name="java.util.Calendar"
xmlType="MyDate"
parseMethod="generated.MyConverter.parseDateTime"
printMethod="generated.MyConverter.printDateTime" />
</jaxb:globalBindings>
</xs:appinfo>
</xs:annotation>
<xs:simpleType name="MyDate">
<xs:restriction base="xs:string" />
</xs:simpleType>
<xs:complexType name="DocType">
<xs:sequence>
<xs:element name="dntz1" type="DateWithoutTimezoneInfo"/>
<xs:element name="aday1" type="xs:date"/>
<xs:element name="dntz2" type="DateWithoutTimezoneInfo"/>
<xs:element name="aday2" type="xs:date"/>
<xs:element name="aday3" type="MyDate"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
And the code for MyGenerator.java (use a better package - NOT generated, and change this in the XML schema, too!):
package generated;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;
import java.util.GregorianCalendar;
import java.util.Calendar;
public class MyConverter {
private static DatatypeFactory dtf;
static {
try {
dtf = DatatypeFactory.newInstance();
} catch( Exception e ){
throw new IllegalStateException( "bad" );
}
}
public static Calendar parseDateTime( String s ){
XMLGregorianCalendar xgc = dtf.newXMLGregorianCalendar( s );
return xgc.toGregorianCalendar();
}
public static String printDateTime( Calendar cal ){
XMLGregorianCalendar xgc =
dtf.newXMLGregorianCalendar( (GregorianCalendar)cal );
return xgc.toString();
}
}
-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
>
>