但我不能完全这样做,因为我反过来,从.XSD生成JAXB bean – 我无法向JAXB bean添加注释,因为它们是生成的代码.
我试过调用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 } }
我生成的JAXB bean的相关部分如下所示:
@XmlSchemaType(name = "date") protected XMLGregorianCalendar activeSince;
当我这样做时,默认日期/ XMLGregorianCalendar编组发生了什么.就好像我没有做到这一切.
任何帮助表示赞赏.
谢谢,
查尔斯
https://jaxb.dev.java.net/guide/Using_different_datatypes.html
JAXB has a built-in table that
determines what Java classes are used
to represent what XML Schema built-in
types,but this can be customized.One of the common use cases for
customization is to replace the
XMLGregorianCalendar with the
friendlier Calendar or Date.
XMLGregorianCalendar is designed to be
100% compatible with XML Schema’s
date/time system,such as providing
infinite precision in sub-seconds and
years,but often the ease of use of
those familiar Java classes win over
the precise compatibility.
该页面并未告诉您如何实际连接自定义,因此请查看如何执行此操作:
http://jaxb.java.net/tutorial/section_5_6_1-Overriding-the-Datatype.html#Overriding%20the%20Datatype
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。