[ERROR] Two declarations cause a collision in the ObjectFactory class

This issue, and its workaround, has been identified by the Unofficial JAXB Guide – Dealing with errors (Project JAXB 2012).

When one of my colleagues and I faced this issue, we still had to spend some amount of time trying to “understand” why, and how to fix it. I thought I’ll share some examples and findings so that in the future people can easily resolve it.

To start with, the error occur because the same name between a TYPE and an ELEMENT. Please refer to the following XML snippet. (The example that is used here is based on the XSD obtained from Personal Property Securities Register).

<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:tns="http://schemas.ppsr.gov.au/2011/04/data" 
    elementFormDefault="qualified" 
    targetNamespace="http://schemas.ppsr.gov.au/2011/04/data" 
    xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <!-- The rest of the code removed -->

    <xs:complexType name="GrantorOrganisation">
        <!-- Content removed -->
    </xs:complexType>
    <xs:element name="GrantorOrganisation"
        nillable="true" type="tns:GrantorOrganisation" />

    <!-- The rest of the code removed -->
</xs:schema>

Notice that the GrantorOrganisation is used as the name for both name and type, hence the collision.

To resolve this, use the JAXB binding and modify the objectFactory method name. Please refer to the bindings.xjb below:

<?xml version="1.0" encoding="UTF-8"?>
<bindings xmlns="http://java.sun.com/xml/ns/jaxb"
          xmlns:xsi="http://www.w3.org/2000/10/XMLSchema-instance"
          xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
          xsi:schemaLocation="http://java.sun.com/xml/ns/jaxb http://java.sun.com/xml/ns/jaxb/bindingschema_2_0.xsd" version="2.1">
    <globalBindings>
        <serializable uid="1" />
    </globalBindings>
    <bindings schemaLocation="schemas.ppsr.gov.au.2011.04.data.xsd">
        <bindings node=".//xs:element[@name='GrantorOrganisation']">
            <factoryMethod name="GrantorOrganisationElement" />
        </bindings>
        <!-- rest of the code remove, need to do it for every collision -->
    </bindings>
</bindings>

Some findings:

  • I was under impression that since it is collision, I can also bind the type instead. So the node will be node=".//xs:complexType[@name='GrantorOrganisation']". That did NOT work.
  • Since the node argument has the prefix xs:, also need to add the xs in the declaration of my binding file (Even thought I did not use it anywhere else).

P.S. Many thanks to George Gao for sharing the solution.

References:

Project JAXB, 2012, ‘Unofficial JAXB Guide – Dealing with errors’, accessed 08 March 2012.

Personal Property Securities Register, 2012, Accessing the PPSR Business to Government (B2G) Channel, accessed 08 March 2012.

Advertisements