【问题标题】:Problems with Jaxb generationJaxb 生成问题
【发布时间】:2016-07-11 13:25:08
【问题描述】:

我的 JAXB 代有问题。我有两个 XSD(都在同一个层次结构中),它们的模式定义非常相似:

A.xsd

<xs:schema>
  <xs:element name="A">
    <xs:complexType>
        <xs:sequence>
          <xs:element name="CacheInfo">
            <xs:complexType>
              <xs:complexContent>
                <xs:extension base="CacheType">
                  <xs:sequence ... />
                </xs:extension>
              </xs:complexContent>
            </xs:complexType>
         </xs:element> <!--CacheInfo -->
       </xs:sequence>
     </xs:complexType>
  </xs:element> <!-- A -->
  <xs:complexType name="CacheType" ... />
  <xs:complexType name="TimeType" ... />
</xs:schema>

B.xsd

<xs:schema>
  <xs:element name="B">
    <xs:complexType>
      <xs:sequence>
        <xs:choice>
          <xs:element name="CacheInfo" type="CacheType">
         </xs:element> <!--CacheInfo -->
        <xs:choice>
      </xs:sequence>
    </xs:complexType>
  </xs:element> <!-- B -->
  <xs:complexType name="CacheType" ... />
  <xs:complexType name="TimeType" ... />
</xs:schema>

这两个 XSD 中 CacheType 的结构是不同的。只是名称相同。

我现在的问题是,当我尝试生成代码时,我得到了这个错误: [错误] 文件:A.xsd [95,38] org.xml.sax.SAXParseException;系统ID:文件:A.xsd;行号:95;列号:38; 'CacheType' 已定义(TimeType 的问题相同)

当我删除其中一个文件时,生成正常。我无法编辑 XSD,因此我需要一个绑定文件来重命名这两种特殊情况的类型:

<bindings schemaLocation="../xsd/A.xsd" node="//xs:complexType[@name='CacheType']">
        <class name="ACacheType" />
</bindings>
<bindings schemaLocation="../xsd/B.xsd" node="//xs:complexType[@name='CacheType']">
        <class name="BCacheType" />
</bindings>

但这不起作用。 当我尝试将类型绑定到属性时,它也不起作用(我最终会遇到同样的错误):

<bindings schemaLocation="../xsd/A.xsd">
    <bindings node="//xs:complexType[@name='CacheType']">
        <property name="ACacheType" />
    </bindings>
    <bindings node=".//xs:complexType[@name='TimeType']">
        <property name="ATimeType" />
    </bindings>
</bindings>
<bindings schemaLocation="../xsd/B.xsd">
    <bindings node="//xs:complexType[@name='CacheType']">
        <property name="BCacheType" />
    </bindings>
    <bindings node=".//xs:complexType[@name='TimeType']">
        <class name="BTimeType" />
    </bindings>
</bindings>

有什么我看不到的吗?为什么我不能使用这些绑定生成这两个 XSD? 为了完成这个,这里是我的 pom.xml sn-p:

<plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>jaxb2-maven-plugin</artifactId>
                <version>2.2</version>
                <executions>
                    <execution>
                        <id>generate-htng-sources</id>
                        <phase>generate-sources</phase>
                        <goals>
                            <goal>xjc</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

【问题讨论】:

  • 可能检查明显:绑定文件是否位于目录/src/main/xjb 中?该插件在该目录中查找绑定文件。如果它们位于不同的位置,则插件需要显式配置。请参阅plugin documentation 中的示例 6。

标签: maven xsd jaxb


【解决方案1】:

您需要在它们自己的包中定义它们 这将生成相同的类,但在不同的包中,所以不会有问题。

<jaxb:bindings schemaLocation="../xsd/A.xsd">
    <jaxb:schemaBindings>
        <jaxb:package name="packagea" />
    </jaxb:schemaBindings>
</jaxb:bindings>

<jaxb:bindings schemaLocation="../xsd/B.xsd">
    <jaxb:schemaBindings>
        <jaxb:package name="packageb" />
    </jaxb:schemaBindings>
</jaxb:bindings>

我没用过这个插件。我使用以下内容:

        <plugin>
            <groupId>org.jvnet.jaxb2.maven2</groupId>
            <artifactId>maven-jaxb2-plugin</artifactId>
            <version>0.12.1</version>
            <executions>
                <execution>
                    <goals>
                        <goal>generate</goal>
                    </goals>
                    <configuration>
                        <schemaDirectory>src/main/resources/xsd</schemaDirectory>
                        <bindingDirectory>src/main/resources/xsd</bindingDirectory>
                        <bindingIncludes>
                            <include>*.xjb</include>
                        </bindingIncludes>
                        <generateDirectory>${project.build.directory}/generated-sources</generateDirectory>
                    </configuration>
                </execution>
            </executions>
        </plugin>

你也可以试试看是否有错误。

【讨论】:

  • 那根本没有帮助 :( 这是我的 binding.xjb:&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;bindings xmlns="http://java.sun.com/xml/ns/jaxb" xmlns:xsi="http://www.w3.org/2000/10/XMLSchema-instance" xmlns:xs="http://www.w3.org/2001/XMLSchema" version="1.0"&gt; &lt;bindings schemaLocation="../xsd/A.xsd" node="/xs:schema"&gt; &lt;schemaBindings&gt; &lt;package name="packagea" /&gt; &lt;/schemaBindings&gt; &lt;/bindings&gt; &lt;bindings schemaLocation="../xsd/B.xsd" node="/xs:schema"&gt; &lt;schemaBindings&gt; &lt;package name="packageb" /&gt; &lt;/schemaBindings&gt; &lt;/bindings&gt; &lt;/bindings&gt;
  • 我已经用过一千次更复杂的 xsds 和许多共同的类,从来没有遇到过问题。
  • 有机会分享你的项目吗?
  • 很遗憾没有@sharing 该项目。 maven-jaxb2-plugin 不起作用。我不得不更改我的整个目录来尝试这个,然后我的 xsds 丢失了它们的包含定义。
  • 无法为您提供更多帮助。这是我用过很多次没有任何问题的东西。我有同样的问题,但定义不同的包,问题就消失了。
【解决方案2】:

首先,我会确保绑定文件位于jaxb2-maven-plugin 预期的位置。在我阅读文档时,即使不更改插件,@Apostolos 提到的使用 schemaBinding 指定包也应该可以工作。

如果正在应用绑定文件,那么我注意到的一件事是原始问题中提到的绑定使用了propertyclass 的混合:

<bindings schemaLocation="../xsd/A.xsd">
  <bindings node="//xs:complexType[@name='CacheType']">
    <property name="ACacheType" />                         <!-- property -->
  </bindings>
  <bindings node=".//xs:complexType[@name='TimeType']">
    <property name="ATimeType" />                          <!-- property -->
  </bindings>
</bindings>
<bindings schemaLocation="../xsd/B.xsd">
  <bindings node="//xs:complexType[@name='CacheType']">
    <property name="BCacheType" />                         <!-- property -->
  </bindings>
  <bindings node=".//xs:complexType[@name='TimeType']">
    <class name="BTimeType" />                             <!-- class -->
  </bindings>
</bindings>

This document from Oracle 在底部附近有一个关于解决冲突的部分。一个示例显示了一个名为“Class”的元素,它是 Java 保留字。他们通过指定属性 类来解决冲突:

<jxb:bindings node="//xs:element[@name='Class']">
  <jxb:class name="Clazz"/>
  <jxb:property name="Clazz"/>
</jxb:bindings> 

所以我想知道,如果修改绑定以指定两者,它是否有效?

<bindings schemaLocation="../xsd/A.xsd">
  <bindings node="//xs:complexType[@name='CacheType']">
    <class name="ACacheType" />
    <property name="ACacheType" />
  </bindings>
  ... repeat for TimeType ....
</bindings>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-05-17
    • 1970-01-01
    • 1970-01-01
    • 2021-08-23
    • 2012-10-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多