我需要将 XML 内容绑定(bind)到我的 pojo 的 String 属性,为此我创建了自定义 DomHandler 来提取所需的部分,如下所示
<sample>
<color>red</color>
<content>
<p>here is content <b>with bold</b></p>
</content>
</sample>
哪个会映射到pojo
@XmlRootElement
class Sample {
@MyCustomAnnotation(value="abcde")
@XmlElement(name="color")
private String color;
@MyCustomAnnotation(value="12345")
@XmlElement(name="content")
@XmlAnyElement(ContentHandler.class)
private String content;
}
我正在使用 XSD 生成我的 pojo,我还有一些自定义注释要添加到生成的 pojo,因此我正在使用 maven-jaxb2-plugin。
问题是,它根据需要添加注释以及与@XmlAnyElement 互斥的@XmlElement,有什么方法可以避免使用XSD 添加@XmlElement 注释?
最佳答案
您可以使用xjc:dom
告诉XJC 您希望将此元素作为DOM 元素来处理。示例:
<jaxb:bindings
schemaLocation="http://schemas.opengis.net/owc/0.3.1/owsContext.xsd"
node="/xs:schema">
<jaxb:schemaBindings>
<jaxb:package name="net.opengis.owc.v_0_3_1"/>
</jaxb:schemaBindings>
<jaxb:bindings node="xs:complexType[@name='LayerType']//xs:element[@ref='kml:Document']">
<xjc:dom/>
</jaxb:bindings>
</jaxb:bindings>
关于java - 从 XSD 生成 pojo 时删除 @XmlElement 注释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46715440/