通过自定义解决JAXB“ObjectFactory类中的名称冲突”问题

时间:2011-09-15 21:55:03

标签: java xml jaxb

these xsd files上执行“xjc”命令会返回Two declarations cause a collision in the ObjectFactory class,因为有2个名为“Scale”和“scale”的元素。

根据this page,可以通过使用<factoryMethod>自定义xsd文件来解决此问题。

你知道怎么做吗?你有一个绑定文件的例子吗?

4 个答案:

答案 0 :(得分:11)

您可以使用bindings标签。将schemaLocation属性设置为特定xsd的位置。子标签,schemaBindings和包然后为该xsd定义新的包名称空间。下面是我刚才和xjc一起使用的绑定文件。如果它仍然有效,那很好。如果没有,例如=)。

<?xml version="1.0" ?>
<bindings xmlns="http://java.sun.com/xml/ns/jaxb" version="2.1"
      xmlns:kml="http://www.opengis.net/kml/2.2"
      xmlns:atom="http://www.w3.org/2005/Atom">

  <bindings schemaLocation="ogckml22.xsd">
    <schemaBindings>
      <package name="net.opengis.kml"/>
    </schemaBindings>
  </bindings>
  <bindings schemaLocation="kml22gx.xsd">
    <schemaBindings>
      <package name="net.opengis.kml.ex"/>
    </schemaBindings>
  </bindings>
  <bindings schemaLocation="atom-author-link.xsd">
    <schemaBindings>
      <package name="org.w3c.atom"/>
    </schemaBindings>
  </bindings>
  <bindings schemaLocation="xAL.xsd">
    <schemaBindings>
      <package name="org.oasis.xal"/>
    </schemaBindings>
  </bindings>
  <bindings scd="kml:scale">
    <class name="scaleliteral"/>
  </bindings>
  <bindings scd="kml:snippet">
    <class name="snippetliteral"/>
  </bindings>
  <bindings scd="kml:Snippet">
    <property name="snippetDeprecated"/>
  </bindings>
  <bindings scd="atom:link">
    <property name="atomLink"/>
  </bindings>
</bindings>

答案 1 :(得分:5)

你试过-B-XautoNameResolution吗?它可能并不总能解决问题,但最好在编写绑定文件之前尝试一下。

答案 2 :(得分:3)

您的问题的一个解决方案是使用-p选项为每个xsd文件指定单独的包名称。

答案 3 :(得分:0)

嗯,经过长时间的尝试,以下配置对我有用。我正在使用jaxb2-maven-plugin 2.3.1和一个单独的绑定文件:

<强>的pom.xml

<build>
    <pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                </configuration>
            </plugin>
        </plugins>
    </pluginManagement>
    <plugins>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>jaxb2-maven-plugin</artifactId>
            <version>2.3.1</version>
            <executions>
                <execution>
                    <id>xjc</id>
                    <phase>generate-sources</phase>
                    <goals>
                        <goal>xjc</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <target>2.1</target>
                <sources>
                    <source>src/main/xsd</source>
                </sources>
                <xjbSources>
                    <xjbSource>src/main/xjb/bindings-1.3.xjb</xjbSource>
                </xjbSources>
                <arguments>
                    <argument>-Xfluent-api</argument>
                </arguments>
                <!-- Package name of the generated sources. -->
                <!-- Don't use it, because we've already declared separate package name inside bindings -->
                <!-- <packageName>com.abc.xyz.generated</packageName> -->
                <outputDirectory>${basedir}/target/generated-sources/classes</outputDirectory>
                <clearOutputDir>false</clearOutputDir>
                <extension>true</extension>
            </configuration>
            <dependencies>
                <dependency>
                    <groupId>org.jvnet.jaxb2_commons</groupId>
                    <artifactId>jaxb2-fluent-api</artifactId>
                    <version>3.0</version>
                </dependency>
            </dependencies>
        </plugin>
    </plugins>
</build>

bindings.xml

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>

<jxb:bindings schemaLocation="../xsd/ccv1p3_imscp_v1p2_v1p0.xsd"
    node="//xs:schema">
    <jxb:globalBindings>
        <jxb:serializable uid="1" />
        <jxb:javaType name="java.util.Calendar" xmlType="xs:dateTime"
            parseMethod="javax.xml.bind.DatatypeConverter.parseDateTime"
            printMethod="javax.xml.bind.DatatypeConverter.printDateTime" />
    </jxb:globalBindings>
    <jxb:schemaBindings>
        <jxb:package name="com.abc.xyz.generated.manifest" />
    </jxb:schemaBindings>
</jxb:bindings>

<jxb:bindings
    schemaLocation="http://www.imsglobal.org/profile/cc/ccv1p3/LOM/ccv1p3_lommanifest_v1p0.xsd"
    node="//xs:schema">
    <jxb:schemaBindings>
        <jxb:package name="com.abc.xyz.generated.lommanifest" />
    </jxb:schemaBindings>
</jxb:bindings>

<jxb:bindings
    schemaLocation="http://www.imsglobal.org/profile/cc/ccv1p3/LOM/ccv1p3_lomresource_v1p0.xsd"
    node="//xs:schema">
    <jxb:schemaBindings>
        <jxb:package name="com.lcs.thincc.generated.lomresource" />
    </jxb:schemaBindings>
</jxb:bindings>

请注意,我们不需要在packageName内指定pom.xml,而是可以在 bindings.xjb 文件中使用<jxb:schemaBindings>声明它这提到here更容易。