'Reuse xsd nested element without changing JAXB generated class

I have an existing XML schema with a nested complex type. I use JAXB to generate java classes from the xsd files. Here's a simplified example, the nested complex type is Child1 in RootElement1.xsd.

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="RootElement1">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="Child1">
                    <xs:complexType>
                        <xs:attribute name="Attr1" use="required"/>
                    </xs:complexType>
                </xs:element>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>

JAXB generates a RootElement1.java file from this, the Child1 element is a static nested class of RootElement1.

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
    "child1"
})
@XmlRootElement(name = "RootElement1")
public class RootElement1 {

    @XmlElement(name = "Child1", required = true)
    protected RootElement1 .Child1 child1;

    public RootElement1 .Child1 getChild1() {
        return child1;
    }

    public void setChild1(RootElement1 .Child1 value) {
        this.child1 = value;
    }

    @XmlAccessorType(XmlAccessType.FIELD)
    @XmlType(name = "")
    public static class Child1 {

        @XmlAttribute(name = "Attr1", required = true)
        @XmlSchemaType(name = "anySimpleType")
        protected String attr1;

        public String getAttr1() {
            return attr1;
        }

        public void setAttr1(String value) {
            this.attr1 = value;
        }
    }
}

These JAXB generated classes are then used in the codebase, by other developers. Some time later I would now like to reuse the Child1 element in another schema file.

If I make Child1 a complex type in order to reuse it:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:complexType name="ChildType">
        <xs:attribute name="Attr1" use="required"/>
    </xs:complexType>
    <xs:element name="RootElement1">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="Child1" type="ChildType"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>

With JAXB the Child1 definition is now removed from RootElement1.java and is defined in a separate ChildType.java file.

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "ChildType")
public class ChildType {

    @XmlAttribute(name = "Attr1", required = true)
    @XmlSchemaType(name = "anySimpleType")
    protected String attr1;

    public String getAttr1() {
        return attr1;
    }

    public void setAttr1(String value) {
        this.attr1 = value;
    }
}

This will break any of the external code that previously called the public getter: getChild1() or setter: setChild1(), would expect the type: RootElement1.Child1. Using the complex type, the external code now would have to use a ChildType.

The example above is much simplified, but still valid. Is there any way to make the Child1 element reusable, but also not break the external code that uses the existing JAXB generated classes?

It is possible to via a bindings.xjb file or other method to force ChildType to be a specific static inner class (in this example of the RootElement1 class)?

I would rather not have to duplicate the definition of Child1 in the other schema, as in the real world (not this simplified example), it is a much more complicated type.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source