我正在尝试从DTD生成的单个~7,000行cXML模式文件生成代码。我已经能够解决一些与xjc自定义的冲突,但最后一个让我感到难过。
我尝试过类和工厂方法自定义但没有成功。
当我尝试应用工厂方法自定义时,我结束了以下消息:
[xjc] [ERROR] compiler was unable to honor this class customization. It is attached to a wrong place, or its inconsistent with other bindings.
其他人遇到过这个问题,并且已经能够解决它。 (This link很有帮助。)
我想知道我的问题是否与元素的抽象性质有关。
这种绕道令人沮丧,任何帮助都会非常感激。
以下是架构中的片段:
<xs:element name="cxml.payment" abstract="true">
<xs:complexType> <!-- Line 2477 -->
<xs:sequence>
<xs:element minOccurs="0" ref="PostalAddress"/>
</xs:sequence>
<xs:attributeGroup ref="PCard.attlist"/>
</xs:complexType>
</xs:element>
<xs:complexType name="cxml.payment">
<xs:sequence>
<xs:element ref="cxml.payment"/>
</xs:sequence>
</xs:complexType>
<xs:element name="Payment" type="cxml.payment" />
<xs:element name="PCard" substitutionGroup="cxml.payment"/>
<xs:attributeGroup name="PCard.attlist">
<xs:attribute name="number" use="required" type="number"/>
<xs:attribute name="expiration" use="required" type="date"/>
<xs:attribute name="name" type="string"/>
</xs:attributeGroup>
以下是原始错误:
> compile:
> [echo] Compiling the schema...
> [xjc] Compiling file:/..cXML.xsd
> [xjc] [ERROR] A class/interface with the same name "cXML.CxmlPayment" is already in use. Use a class customization to
> resolve this conflict.
> [xjc] line 2477 of file:..cXML.xsd
> [xjc]
> [xjc] [ERROR] (Relevant to above error) another "CxmlPayment" is generated from here.
> [xjc] line 2477 of file:..cXML.xsd
> [xjc]
> [xjc] [ERROR] (Relevant to above error) This confusing error happened most likely because the schema uses a technique called
> "chameleon schema", which causes a single definition to be loaded
> multiple times into different namespaces. See
> http://forums.java.net/jive/thread.jspa?threadID=18631 for more about
> this.
> [xjc] line 2477 of file:..cXML.xsd
> [xjc]
> [xjc] [ERROR] Two declarations cause a collision in the ObjectFactory class.
> [xjc] line 2477 of file:..cXML.xsd
> [xjc]
> [xjc] [ERROR] (Related to above error) This is the other declaration.
> [xjc] line 2477 of file:..cXML.xsd
> [xjc]
> [xjc] failure in the XJC task. Use the Ant -verbose switch for more details
答案 0 :(得分:3)
当然,我应该想通了。我一发布问题解决方案就出现了。
根据JAXB "default binding rules",绑定编译器尝试为每个Element创建一个Java类,并且每个都命名为ComplexType。引起冲突的原因是两个名称都使用了cxml.payment。
在这种情况下,只需要这个简单的类定制:
<jxb:bindings node="//xs:element[@name='cxml.payment']/xs:complexType">
<jxb:class name="CxmlInnerPaymentType" />
</jxb:bindings>
课程是密切关注xjc错误消息中的行号。在这种情况下,他们一直指着我2477行。