如果已经创建,从解组中排除 xsd 元素?

Excluding xsd element from unmarshalling if already created?

使用 jaxb(maven-jaxb2-plugin) 编译器构建 maven 时出现以下错误

A class/interface with the same name "org.somePackage.customer" is already in use.
    Use a class customization to resolve this conflict.
        at com.sun.tools.xjc.util.CodeModelClassFactory.createClass(CodeModelClassFactory.java:121)
        at com.sun.tools.xjc.util.CodeModelClassFactory.createClass(CodeModelClassFactory.java:82)
        at com.sun.tools.xjc.generator.bean.ImplStructureStrategy.createClasses(ImplStructureStrategy.java:82)
        at com.sun.tools.xjc.generator.bean.BeanGenerator.generateClassDef(BeanGenerator.java:437)
        at com.sun.tools.xjc.generator.bean.BeanGenerator.getClazz(BeanGenerator.java:469)
        at com.sun.tools.xjc.generator.bean.BeanGenerator.<init>(BeanGenerator.java:194)
        at com.sun.tools.xjc.generator.bean.BeanGenerator.generate(BeanGenerator.java:166)
        at com.sun.tools.xjc.model.Model.generateCode(Model.java:290)
        at org.jvnet.mjiip.v_2_2.XJC22Mojo.generateCode(XJC22Mojo.java:70)

相关的 XSD 代码片段,因为它会出现错误

 <xsd:element name="customer" >
         ........
 </xsd:element>
 <xsd:element name="permanentCustomer" type="customer"/>

maven-jaxb2-plugin 的相关 pom 部分是

    <plugin>
            <groupId>org.jvnet.jaxb2.maven2</groupId>
            <artifactId>maven-jaxb2-plugin</artifactId>
            <version>0.8.3</version>
            <configuration>
                <schemaDirectory>src/main/resources/META-INF/schema</schemaDirectory>
            </configuration>
            <executions>
                <execution>
                    <goals>
                        <goal>generate</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

有效方法

如果我介绍 block="substitution" 它有效

<xsd:element name="customer" block="substitution">
 ........
</xsd:element>
<xsd:element name="permanentCustomer" type="customer"/>

在网上进行研究后,我猜 jaxb 编译器再次尝试基于元素 name="permanentCustomer" 创建客户 class,它已经基于元素 name ="customer". 创建了,所以我不想在解组时为 permanentCustomer 创建 java 对象,因为它已经在创建客户时创建。

我能想到两个解决办法

1)maven-jaxb2-plugin插件的一些配置,如果java class已经生成,不要重新生成继续

2) 或者在 xml 级别提及某些属性以忽略特定元素?

是否存在任何配置?

这是一篇 question, not specific to ,我 (diclaimer) 恰好是其作者。

忽略为 customer 元素生成 Customer class 因为它已经为 customer 类型生成不是一个好主意,因为它们是不同的概念。治标不治本。

核心问题是 XJC 尝试为 customer 元素创建 class,这会导致命名冲突。因此,要么不为 customer 元素生成 class,要么对命名冲突做些事情。

我不确定为什么 XJC 会尝试为 customer 元素创建 class。通常情况下不应如此。您可能需要为此进行一些配置,不太确定。

解决命名冲突很容易。你可能会这样做:

<jaxb:bindings 
    schemaLocation="..." 
    node="/xs:schema">

    <jaxb:bindings node="xs:element[@name='customer']">
        <jaxb:class name="CustomerElement"/>
    </jaxb:bindings>
</jaxb:bindings>

或者,您可以使用全局自定义进行名称转换,请参阅此答案:

类似于: