0

I currently have an XSD that looks something like this,

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
  <xs:element name="apiCategoryResponse">
    <xs:complexType>
      <xs:sequence>
        <xs:element ref="category"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
  <xs:element name="category">
    <xs:complexType>
      <xs:sequence>
        <xs:element ref="version"/>
        <xs:element ref="name"/>
        <xs:element ref="desc"/>
        <xs:element ref="id"/>
        <xs:element maxOccurs="unbounded" ref="subCategory"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
  <xs:element name="subCategory">
    <xs:complexType>
      <xs:sequence>
        <xs:element maxOccurs="unbounded" ref="childCategory"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
  <xs:element name="version" type="xs:integer"/>
  <xs:element name="name" type="xs:NCName"/>
  <xs:element name="desc" type="xs:NCName"/>
  <xs:element name="id" type="xs:integer"/>
  <xs:element name="childCategory">
    <xs:complexType>
      <xs:choice minOccurs="0" maxOccurs="unbounded">
        <xs:element ref="childCategory"/>
        <xs:element ref="desc"/>
        <xs:element ref="id"/>
        <xs:element ref="name"/>
        <xs:element ref="version"/>
      </xs:choice>
    </xs:complexType>
  </xs:element>
</xs:schema>

When I use XJC to generate the Java code the ChildCategory generates something like this,

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
    "childCategoryOrDescOrId"
})
@XmlRootElement(name = "childCategory")
public class ChildCategory {

    @XmlElementRefs({
        @XmlElementRef(name = "name", type = JAXBElement.class),
        @XmlElementRef(name = "id", type = JAXBElement.class),
        @XmlElementRef(name = "version", type = JAXBElement.class),
        @XmlElementRef(name = "childCategory", type = ChildCategory.class),
        @XmlElementRef(name = "desc", type = JAXBElement.class)
    })
    protected List<Object> childCategoryOrDescOrId;

    /**
     * Gets the value of the childCategoryOrDescOrId property.
     * 
     * <p>
     * This accessor method returns a reference to the live list,
     * not a snapshot. Therefore any modification you make to the
     * returned list will be present inside the JAXB object.
     * This is why there is not a <CODE>set</CODE> method for the childCategoryOrDescOrId property.
     * 
     * <p>
     * For example, to add a new item, do as follows:
     * <pre>
     *    getChildCategoryOrDescOrId().add(newItem);
     * </pre>
     * 
     * 
     * <p>
     * Objects of the following type(s) are allowed in the list
     * {@link JAXBElement }{@code <}{@link BigInteger }{@code >}
     * {@link JAXBElement }{@code <}{@link BigInteger }{@code >}
     * {@link JAXBElement }{@code <}{@link String }{@code >}
     * {@link ChildCategory }
     * {@link JAXBElement }{@code <}{@link String }{@code >}
     * 
     * 
     */
    public List<Object> getChildCategoryOrDescOrId() {
        if (childCategoryOrDescOrId == null) {
            childCategoryOrDescOrId = new ArrayList<Object>();
        }
        return this.childCategoryOrDescOrId;
    }

}

And the issue is that I don't want it be a list for the desc/id/name/etc.

I want individual setters and getters for those fields while the childCategory is a list that can contain another childCategory list as well as it's own desc/name/version etc values. Basically I want explicit setters and getters to be auto generated from XJC. All else fails I can always manually code it but I was wondering if there was some tricks to force XJC to generate the code the way I want it to.

1 Answers1

2

why don't you do the same thing that you have done for category

<xs:element name="childCategory">
  <xs:complexType>
    <xs:sequence>
      <xs:element ref="desc" />
      <xs:element ref="id" />
      <xs:element ref="name" />
      <xs:element ref="version" />
      <xs:element ref="childCategory" minOccurs="0" maxOccurs="unbounded" />
    </xs:sequence>
  </xs:complexType>
</xs:element>
shyam
  • 8,153
  • 4
  • 23
  • 40