wmgeiger101x
wmgeiger101x

Reputation: 153

Binding nested classes generating a new nested class called 'Type', how can this be prevented

I have an XSD (that I cannot change) that contains these elements defined in a complexType's sequence element:

<xsd:element minOccurs="0" name="precision" nillable="true">
      <xsd:complexType>
                   ...
      </xsd:complexType>
 </xsd:element>
 <xsd:element minOccurs="0" name="Precision" nillable="true">
      <xsd:complexType>
                    ...
      </xsd:complexType>
 </xsd:element>          

When I run xjc over the schema, I get exceptions over the name conflicts in the ObjectFactory and the nested classes.

To attempt to fix this, I added this external binding for mapping the fields to unique names and classes to unique names:

<jaxb:bindings node="//*[local-name()='element' and @name='precision']" schemaLocation="schema.xsd">
    <jaxb:property name="precision1"/>
    <jaxb:class name="precision1"/>
</jaxb:bindings>
<jaxb:bindings node="//*[local-name()='element' and @name='Precision']" schemaLocation="schema.xsd">
    <jaxb:property name="Precision2"/>
    <jaxb:class name="Precision2"/>
</jaxb:bindings>

This enabled the generation of the JAXB classes but, the classes get generated like this:

 @XmlElementRef(name = "precision1", namespace = "namespace.CustomerInvoice2", type = JAXBElement.class, required = false)
protected JAXBElement<CustomerOrderDate.Precision1> precision1;

    protected final static QName NAME = new QName("namespace.CustomerInvoice3", "precision");

    public Precision11(CustomerOrderDate.Precision11 .Type value) {
        super(NAME, ((Class) CustomerOrderDate.Precision11 .Type.class), CustomerOrderDate.class, value);
    }

    public Precision11() {
        super(NAME, ((Class) CustomerOrderDate.Precision11 .Type.class), CustomerOrderDate.class, null);
    }


    /**
     * whole bunch of comments...
     */
    @XmlAccessorType(XmlAccessType.FIELD)
    @XmlType(name = "", propOrder = {
        "precision",
        "use"
    })
    public static class Type {
         //The actual fields

BUT, if I manually change the field names to precision1 and Precision2, the classes get generated as:

 public static class Precision1 {

    @XmlElementRef(name = "precision", namespace = "namespace.CustomerInvoice2", type = JAXBElement.class, required = false)
    protected JAXBElement<Integer> precision;
    @XmlElementRef(name = "use", namespace = "namespace.CustomerInvoice2", type = JAXBElement.class, required = false)
    protected JAXBElement<String> use;
    @XmlAttribute(name = "mark")
    protected String mark;
    @XmlAttribute(name = "mode")
    protected String mode;
    @XmlAttribute(name = "invalid")
    protected Boolean invalid;
    // blah, blah, blah...

I was curious if there is a binding that I could use that would keep the QName values in the root class's field declarations and keep the Type information within the generated class.

Upvotes: 2

Views: 1769

Answers (1)

Edwardth
Edwardth

Reputation: 707

Based on this answer and the tutorial JAXB - Java Architecture for XML Binding.

You only need:

<jaxb:bindings node="//xs:element[@name='precision']/xs:complexType" schemaLocation="schema.xsd">
    <jaxb:class name="Precision1"/>
</jaxb:bindings>
<jaxb:bindings node="//xs:element[@name='Precision']/xs:complexType" schemaLocation="schema.xsd">
    <jaxb:class name="Precision2"/>
</jaxb:bindings>

Upvotes: 2

Related Questions