Generating Arrays for XSD Sequences via JaxB JXC

自古美人都是妖i 提交于 2019-12-10 14:32:55

问题


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.


回答1:


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...



来源:https://stackoverflow.com/questions/17849323/generating-arrays-for-xsd-sequences-via-jaxb-jxc

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!