How to Customize JAXB Marshalling if generating JAXB beans from XML
Posted
by Charles O.
on Stack Overflow
See other posts from Stack Overflow
or by Charles O.
Published on 2010-03-15T18:23:28Z
Indexed on
2010/03/15
18:29 UTC
Read the original article
Hit count: 749
jaxb
Hello,
I want to customize the marshalling of dates in JAXB. It's a variant of this already asked question. I would think I would use an XMLAdapter, as this answer questions specifies.
But I can't do that exactly, because I'm going the other way around, generating the JAXB beans from an .XSD -- I can't add annotations to the JAXB beans because they are generated code.
I've tried calling Marshaller.setAdapter(), but with no luck.
final Marshaller marshaller = getJaxbContext().createMarshaller();
marshaller.setSchema(kniSchema);
marshaller.setAdapter(new DateAdapter());
...
private static class DateAdapter extends XmlAdapter<String, XMLGregorianCalendar> {
@Override
public String marshal(XMLGregorianCalendar v) throws Exception {
return "hello"; //Just a test to see if it's working
}
@Override
public XMLGregorianCalendar unmarshal(String v) throws Exception {
return null; // Don't care about this for now
}
}
Where the relevant part of my generated JAXB bean looks like this:
@XmlSchemaType(name = "date")
protected XMLGregorianCalendar activeSince;
When I do this, what the default date/XMLGregorianCalendar marshalling happens. It's as if I didn't do it all.
Any help is appreciated.
Thanks,
Charles
© Stack Overflow or respective owner