【问题标题】:Remove @XmlElement annotation while generating pojo from XSD从 XSD 生成 pojo 时删除 @XmlElement 注释
【发布时间】:2017-10-12 17:19:48
【问题描述】:

我需要将 XML 内容绑定到我的 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 中,因此我正在使用 ma​​ven-jaxb2-plugin

问题是,它会根据需要添加注解以及与@XmlAnyElement 互斥的@XmlElement,有什么方法可以避免使用XSD 添加@XmlElement 注解?

【问题讨论】:

    标签: java xsd jaxb annotations maven-jaxb2-plugin


    【解决方案1】:

    您可以使用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>
    

    【讨论】:

    • 在 XmlElement 上方使用已被删除,但现在的问题是插件无法将我的自定义 DomHandler 添加到注释中,并且出现异常“它附加到错误的位置,或者它与其他绑定不一致"
    • 发生上述情况是因为我正在尝试使用我的自定义 DomHandler 更新 XmlAnyElement。
    • 例如,我的 bindings.xjb 下面有
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-05-05
    • 2018-11-20
    • 2019-08-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-05
    • 2021-03-25
    相关资源
    最近更新 更多