Solve JAXB "name clash in ObjectFactory" class with customization

Running the "xjc" command in these xsd files returns Two declarations cause a collision in the ObjectFactory class , because there are two elements named "Scale" and "scale".

According to this page , this problem can be solved by configuring xsd files with <factoryMethod> .

Do you know how to do this? Do you have an example file binding for this?

+8
java xml jaxb
source share
4 answers

You can use the bindings tag. Set the schemaLocation attribute to the location of the specific xsd. The child, schemaBindings, and package tags then define a new package namespace for this xsd. Below is the bindings file that I used with xjc some time ago. If it still works, great. If not, example =).

 <?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> 
+9
source share

Have you tried -B-XautoNameResolution? This may not always do the trick, but it is best to try before you write the binding file.

+5
source share

One solution to your problem is to use the -p option to specify separate package names for each xsd file.

+3
source share

Well, after long attempts, the following configuration worked for me. I am using jaxb2-maven-plugin 2.3.1 and a separate bind file:

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> 

Note that we do not need to specify packageName inside pom.xml , instead we can declare it using <jxb:schemaBindings> inside the bindings.xjb file , which is much simpler, as mentioned here .

0
source share

All Articles