Search code examples
jaxbxsdjaxb2

Generating jaxb classes with naming conflicts


I have created a new JAXB project and imported an xsd which I am trying to generate JAXB classes off of. When I select "Generate->JAXB Classes" I get a number of similar errors all which relate to naming conflicts:

 A class/interface with the same name "generated.Document" is already in use. Use a class customization to resolve this conflict.

The above error is thrown from the following piece of the .xsd file

 <xs:complexType name="Document">
    <xs:sequence>
      <xs:element ref="Document"/>
    </xs:sequence>
 </xs:complexType>
 <xs:element name="Document">
 ...

Changing the .xsd file is not an option for me so I have to find another way. I have been reading about the possibility of creating a bindings file. I can't seem to find good documentation on how to do that for my particular problem. My three main questions are:

  • Should I create a new bindings .xjb file and add it to eclipse to solve this issue?
  • How should this bindings file look in order to resolve the "Document" naming conflicts?
  • How do I get eclipse to recognize the bindings file when I run "Generate -> JAXB Classes

Solution

    • Should I create a new bindings .xjb file and add it to eclipse to solve this issue?

    yes, you can with an xjb file

    • How should this bindings file look in order to resolve the "Document" naming conflicts?

    (e.g. I don't know your entire xsd, this is only an example)

    <bindings version="2.0" xmlns="http://java.sun.com/xml/ns/jaxb"
        xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
        xmlns:annox="http://annox.dev.java.net">
    
            <bindings node="xs:complexType[@name='Document']">
                <class name="Item"/>
            </bindings>
    
    </bindings>
    
    • How do I get eclipse to recognize the bindings file when I run "Generate -> JAXB Classes

    Eclipse allows you to attach an XJB file (Binding files:)enter image description here

    I hope I've given you all the answers about your question.