如何强制Jaxb将未注释的属性视为属性而不是元素?

时间:2014-12-31 08:24:35

标签: java xml jaxb

拥有一个具有大量属性的类,默认情况下Jaxb将Unanotated属性视为XmlElement,

如何强制它将所有Unanotated属性编码为XmlAttribute?

1 个答案:

答案 0 :(得分:0)

使用JAXB RI,您可以实现自定义注释阅读器并使用JAXB RI上下文进行配置:

final AnnotationReader<Type, Class, Field, Method> annotationReader = new CustomAnnotationReader();

final Map<String, Object> properties = new HashMap<String, Object>();

properties.put(JAXBRIContext.ANNOTATION_READER, annotationReader);

final JAXBContext context = JAXBContext.newInstance(
    "org.jvnet.annox.samples.po",
    Thread.currentThread().getContextClassLoader(),
    properties);

final Object myObject = context.createUnmarshaller().unmarshal( ... );

使用MOXy,您可以使用XML bindings为您的属性配置所需的mappin。

我认为自定义注释阅读器的工作量会减少。