JAXB 2.0 -XJC编译问题

时间:2012-01-09 17:48:50

标签: java xml

我正在使用JAXB 2.0生成POJO。 我有以下XSD

<?xml version="1.0" encoding="UTF-8"?>
  <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
    <xs:element name="test">
      <xs:complexType>
        <xs:attribute name="system" type="xs:string"/>
        <xs:attribute name="dim" type="xs:integer"/>
      </xs:complexType>
    </xs:element>
    <xs:element name="test1" type="xs:string"/>
    <xs:element name="test2" type="xs:string"/>
    <xs:element name="scoring_guide" type="embedded_scoring_guide_type"/>
    <xs:complexType name="embedded_scoring_guide_type">
      <xs:sequence>
        <xs:choice minOccurs="0" maxOccurs="unbounded">
          <xs:element ref="test"/>
          <xs:element ref="test1"/>
        </xs:choice>
        <xs:choice>
          <xs:element ref="test2" maxOccurs="unbounded"/>
        </xs:choice>
        <xs:choice minOccurs="0" maxOccurs="unbounded">
          <xs:element ref="test"/>
          <xs:element ref="test1"/>
        </xs:choice> 
      </xs:sequence>
    </xs:complexType>
</xs:schema>

当我尝试从上面的Schema中生成POJO时。我收到以下错误:

Gennerating jaxb objects for C:\WORKAREA\JAXB\SubSeqenceElementCall\schema\SubSequentElementCall.xsd Using XJC generator...
parsing a schema...

[ERROR] Property "TestOrTest1" is already defined. Use &lt;jaxb:property> to resolve this conflict.
  line 14 of file:/C:/WORKAREA/JAXB/SubSeqenceElementCall/schema/SubSequentElementCall.xsd

[ERROR] The following location is relevant to the above error
  line 21 of file:/C:/WORKAREA/JAXB/SubSeqenceElementCall/schema/SubSequentElementCall.xsd

[ERROR] Element "test1" shows up in more than one properties.
  line 21 of file:/C:/WORKAREA/JAXB/SubSeqenceElementCall/schema/SubSequentElementCall.xsd

[ERROR] The following location is relevant to the above error
  line 14 of file:/C:/WORKAREA/JAXB/SubSeqenceElementCall/schema/SubSequentElementCall.xsd

Failed to parse a schema.
Gennerating jaxb objects for C:\WORKAREA\JAXB\SubSeqenceElementCall\schema\SubSequentElementCall.xsd Completed!

请帮助解决上述问题。

由于 维克拉姆

1 个答案:

答案 0 :(得分:2)

尝试定义JAXB内联属性或绑定文件,或避免使用相同字段的匿名序列/选项。

这是解决问题的JAXB内联绑定示例:

<?xml version="1.0" encoding="UTF-8"?>
  <xs:schema 
    xmlns:xs="http://www.w3.org/2001/XMLSchema" 
    elementFormDefault="qualified" attributeFormDefault="unqualified"
    xmlns:jxb="http://java.sun.com/xml/ns/jaxb" jxb:version="1.0">
    <xs:element name="test">
      <xs:complexType>
        <xs:attribute name="system" type="xs:string"/>
        <xs:attribute name="dim" type="xs:integer"/>
      </xs:complexType>
    </xs:element>
    <xs:element name="test1" type="xs:string"/>
    <xs:element name="test2" type="xs:string"/>
    <xs:element name="scoring_guide" type="embedded_scoring_guide_type"/>
    <xs:complexType name="embedded_scoring_guide_type">
      <xs:sequence>
        <xs:choice minOccurs="0" maxOccurs="unbounded">
            <!-- Additional JAXB annotations -->
            <xs:annotation>
                <xs:appinfo>
                   <jxb:property name="firstBlock"/>
                </xs:appinfo>
            </xs:annotation>
          <xs:element ref="test"/>
          <xs:element ref="test1"/>                    
        </xs:choice>
        <xs:choice>
          <xs:element ref="test2" maxOccurs="unbounded"/>
        </xs:choice>
        <xs:choice minOccurs="0" maxOccurs="unbounded">
            <!-- Additional JAXB annotations -->
            <xs:annotation>
                <xs:appinfo>
                   <jxb:property name="secondBlock"/>
                </xs:appinfo>
            </xs:annotation>                  
          <xs:element ref="test"/>
          <xs:element ref="test1"/>
        </xs:choice> 
      </xs:sequence>
    </xs:complexType>
</xs:schema>

另请参阅Oracle文档http://docs.oracle.com/cd/E17802_01/webservices/webservices/docs/2.0/tutorial/doc/JAXBUsing4.html