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