Generating JAXB class from XSDs with similar attribute names

Oleg Kandaurov picture Oleg Kandaurov · Dec 26, 2011 · Viewed 8.5k times · Source

I use maven-jaxb2-plugin to generate jaxb annotated classes from xsd. I have many xsd files like those:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="A3">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="loginPartner">
          <xs:complexType>
            <xs:sequence>
              <xs:element type="xs:string" name="login"/>
              <xs:element type="xs:string" name="password"/>
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="A3">
    <xs:complexType>
      <xs:sequence>
        <xs:element type="xs:string" name="errorCode"/>
        <xs:element type="xs:string" name="errorDescription"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

When I run maven plugin it gives me an error:

[ERROR] Error while parsing schema(s).Location [ file:schema1.xsd{10,16}]. org.xml.sax.SAXParseException: 'A3' is already defined

Is there any way to fix this? Actually I have many XSDs representing request/response messages to/from server. I want to simplify creating, validating, parsing messages. Maybe is there another solution for this?

Answer

Andrew B picture Andrew B · May 31, 2012

I had a similar problem; I had two separate and independent WSDL (each with several schema definitions in each) that I was running through JAXB (via the maven-jaxb2-plugin) to generate mapping classes.

My WSDL's shared a duplicate schema definition that was causing XJC to choke.

Because they were both independent, I was able to generate JAXB mappings by running two separate executions of the maven-jaxb2-plugin - one for each WSDL (covered here - How can I tell jaxb / Maven to generate multiple schema packages?).