如何从XML生成JAXB bean,自定义JAXB编组

Cha*_* O. 6 jaxb

我想在JAXB中自定义日期的编组.这是这个已被问到的问题的变体.我想我会使用XMLAdapter,因为这个答案问题指明了.

但我不能这样做,就是这样,因为我要去周围的其他方法,从.XSD生成JAXB豆 - 因为它们生成的代码,我不能添加注释JAXB的豆.

我试过调用Marshaller.setAdapter(),但没有运气.

            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
            }
}
Run Code Online (Sandbox Code Playgroud)

我生成的JAXB bean的相关部分如下所示:

    @XmlSchemaType(name = "date")
    protected XMLGregorianCalendar activeSince;
Run Code Online (Sandbox Code Playgroud)

当我这样做时,默认日期/ XMLGregorianCalendar编组发生了什么.就好像我没有做到这一切.

任何帮助表示赞赏.

谢谢,

查尔斯