Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generating Arrays for XSD Sequences via JaxB JXC

I've got a XSD describing some sequences of complex types e.g.

<xs:complexType name="Catalog">
  <xs:sequence>
    <xs:element name="Category" minOccurs="0" maxOccurs="unbounded">
      <xs:complexType>
        <xs:sequence>
          <xs:element type="xs:string" name="ParentCategoryIDRef"/>
          <xs:element type="xs:string" name="Method"/>
        </xs:sequence>
      <xs:complexType>
    </xs:element>
  </xs:sequence>
<xs:complexType>

Now when I use JaxBs XJC to convert this into Java classes it will generate me a java.util.List in my Catalog class for the field and getter/setter of Category.

However, what I need for using it in an Axis2 webservice using java2wsdl are Arrays like Category[].

I'm a bit familiar with JaxB bindings and already tried specifying the collection type using:

<jaxb:property collectionType="Category[]"/>

which resulted in invalid code, because it was still using a java.util.List, but with a constructor new Category[]<Category>.

Of course I can always edit the generated code after generation, but this would cause problems when I try to re-generate it.

What I've got now is:

public class Catalog {
  @XmlElement(name = "Category")
  protected List<Category> category;
}

What I want is:

public class Catalog {
  @XmlElement(name = "Category")
  protected Category[] category;
}

Any ideas? I'm currently using XJC 2.2.6 with Axis2 1.6.2.

like image 393
Peter Ilfrich Avatar asked Nov 12 '22 01:11

Peter Ilfrich


1 Answers

I think you need to use the javaType tag:

<xs:complexType name="catalog">
        <xs:sequence>
            <xs:element name="category" type="ns:Category" >
                <xs:annotation>
                    <xs:appinfo>
                        <jxb:javaType name="Category[]"/>
                    </xs:appinfo>
                </xs:annotation>
            </xs:element>
        </xs:sequence>
    </xs:complexType>

Generates the following class:

public class Catalog {

        @XmlElement(required = true, type = Category.class)
        protected Category[] category;

        public Category[] getCategory() {
            return category;
        }

        public void setCategory(Category[] value) {
            this.category = value;
        }

    }

(Using the org.apache.cxf cxf-xjc-plugin 2.6.2 maven plugin)

You will also need the definition of Category in your XSD...

like image 146
rob Avatar answered Nov 15 '22 11:11

rob