Java Amazon Product.xsd的JAXB类生成不起作用

Java Amazon Product.xsd的JAXB类生成不起作用,java,xsd,jaxb,amazon,Java,Xsd,Jaxb,Amazon,当使用JAXB及其maven插件从Products.xsd生成Java类时,我得到以下警告和错误 org.xml.sax.SAXParseException; systemId: https://images-na.ssl-images-amazon.com/images/G/01/rainier/help/xsd/release_1_9/amzn-base.xsd; lineNumber: 956; columnNumber: 45; Simple type "ProcessorTypeVal

当使用JAXB及其maven插件从Products.xsd生成Java类时,我得到以下警告和错误

org.xml.sax.SAXParseException; systemId: https://images-na.ssl-images-amazon.com/images/G/01/rainier/help/xsd/release_1_9/amzn-base.xsd; lineNumber: 956; columnNumber: 45; Simple type "ProcessorTypeValues" was not mapped to Enum due to EnumMemberSizeCap limit. Facets count: 744, current limit: 256. You can use customization attribute "typesafeEnumMaxMembers" to extend the limit.
  at com.sun.tools.xjc.reader.xmlschema.ErrorReporter.warning(ErrorReporter.java:88)
...
Error while generating code.Location [ https://images-na.ssl-images-amazon.com/images/G/01/rainier/help/xsd/release_1_9/SWVG.xsd{300,40}].
  org.xml.sax.SAXParseException; systemId: https://images-na.ssl-images-amazon.com/images/G/01/rainier/help/xsd/release_1_9/SWVG.xsd; lineNumber: 300; columnNumber: 40; A class/interface with the same name "generated.BBFCRatingType" is already in use. Use a class customization to resolve this conflict.
在我能够修复此问题后,出现了如下错误:

.../target/generated-sources/xjc/generated/Home.java:[2737,45] cannot find symbol
  symbol:   class Home
  location: class generated.Home.ProductType
.../target/generated-sources/xjc/generated/Home.java:[2929,23] class generated.Home is already defined in package generated

在谷歌搜索时,我看到了许多与此相关的问题,但没有人提出适当的解决方案。

经过一天的分析问题并逐步解决后,我让它开始工作

在我看来,XSD不是很一致,例如,它们包含多个元素声明,可以在一个公共XSD中进行总结,并且一些元素具有层次结构,其中包含具有相同名称的元素

当使用JAXBs默认设置时,XSD不是无效的,但在某些方面不符合Java约定

一种解决方案是,将XSD保留在本地,只注释掉冲突部分,然后生成类。 但是我想使用提供的URL生成类,所以这对我来说不起作用

因此,我将在这里发布我的解决方案,也许它有助于某人:

生成是通过pom.xml中的
maven-jaxb2-plugin
完成的,因此这部分内容如下(我的目标包定义为
com.amazon.product
,您可以将其更改为您喜欢的版本):


已生成类。主目录已在生成的包中定义

例如,Home.xsd将名为
Home
的元素定义为
ProductType
的子元素,该子元素本身位于另一个元素中,也称为
Home
。问题与前面的错误相同:一个包中有两个同名的类

我们必须给第二类赋予不同的名称:

<bindings schemaLocation="https://images-na.ssl-images-amazon.com/images/G/01/rainier/help/xsd/release_1_9/Home.xsd" node="//xsd:element[@name='ProductType']/xsd:complexType/xsd:choice/xsd:element[@name='Home']/xsd:complexType">
  <class name="HomeSub"/>
</bindings>


其他想法/改进

如果希望某些无法自动生成的枚举成为真正的Java枚举,则必须手动强制每个枚举。 例如,对于产品服装:

<bindings schemaLocation="xsd/ProductClothing.xsd">
  <bindings node="//xsd:element[@name='VariationData']/xsd:complexType/xsd:sequence">
    <bindings node="./xsd:element[@name='Parentage']/xsd:simpleType">
      <typesafeEnumClass name="Parentage" />
    </bindings>
    <bindings node="./xsd:element[@name='VariationTheme']/xsd:simpleType">
      <typesafeEnumClass name="VariationTheme" />
    </bindings>
  </bindings>
  <bindings node="//xsd:element[@name='ClassificationData']/xsd:complexType/xsd:sequence">
    <bindings node="./xsd:element[@name='ClothingType']/xsd:simpleType">
      <typesafeEnumClass name="ClothingType" />
    </bindings>
    <bindings node="./xsd:element[@name='PerformanceRating']/xsd:simpleType">
      <typesafeEnumClass name="PerformanceRating" />
    </bindings>
    <bindings node="./xsd:element[@name='CupSize']/xsd:simpleType">
      <typesafeEnumClass name="CupSize" />
    </bindings>
    <bindings node="./xsd:element[@name='TargetGender']/xsd:simpleType">
      <typesafeEnumClass name="TargetGender" />
    </bindings>
    <bindings node="./xsd:element[@name='WaterResistanceLevel']/xsd:simpleType">
      <typesafeEnumClass name="WaterResistanceLevel" />
    </bindings>
  </bindings>
</bindings>

更新日期:2017年12月22日
我已经创建了一个包含所有必要绑定的脚本,以使XSD与Java一起工作。

更新:添加以下代码(2016年11月)`````
<globalBindings ... typesafeEnumMaxMembers="1000" .../>
<globalBindings ... underscoreBinding="asCharInWord" .../>
<bindings schemaLocation="https://images-na.ssl-images-amazon.com/images/G/01/rainier/help/xsd/release_1_9/Home.xsd" node="//xsd:element[@name='ProductType']/xsd:complexType/xsd:choice/xsd:element[@name='Home']/xsd:complexType">
  <class name="HomeSub"/>
</bindings>
<bindings schemaLocation="xsd/ProductClothing.xsd">
  <bindings node="//xsd:element[@name='VariationData']/xsd:complexType/xsd:sequence">
    <bindings node="./xsd:element[@name='Parentage']/xsd:simpleType">
      <typesafeEnumClass name="Parentage" />
    </bindings>
    <bindings node="./xsd:element[@name='VariationTheme']/xsd:simpleType">
      <typesafeEnumClass name="VariationTheme" />
    </bindings>
  </bindings>
  <bindings node="//xsd:element[@name='ClassificationData']/xsd:complexType/xsd:sequence">
    <bindings node="./xsd:element[@name='ClothingType']/xsd:simpleType">
      <typesafeEnumClass name="ClothingType" />
    </bindings>
    <bindings node="./xsd:element[@name='PerformanceRating']/xsd:simpleType">
      <typesafeEnumClass name="PerformanceRating" />
    </bindings>
    <bindings node="./xsd:element[@name='CupSize']/xsd:simpleType">
      <typesafeEnumClass name="CupSize" />
    </bindings>
    <bindings node="./xsd:element[@name='TargetGender']/xsd:simpleType">
      <typesafeEnumClass name="TargetGender" />
    </bindings>
    <bindings node="./xsd:element[@name='WaterResistanceLevel']/xsd:simpleType">
      <typesafeEnumClass name="WaterResistanceLevel" />
    </bindings>
  </bindings>
</bindings>