wrapper class missing when using xjc on xsd

a 夏天 提交于 2019-12-12 03:25:38

问题


i have the following XSD:

<xsd:element name="products" >
    <xsd:complexType>
      <xsd:sequence>
        <xsd:element name="product" type="foo:myProduct" maxOccurs="unbounded" />
      </xsd:sequence>
      </xsd:complexType>
</xsd:element>

Now when i issue XJC, it doesnt generate a Products.class file but only the Product.class. But of course my XML looks like this:

<products>
   <product>...</product>
   <product>...</product>
</products>

So at the end, i dont have a class with XmlRootElement annotation, which is odd. Of course i cant get marshalling working.

Any hints what could be wrong with my XSD or what do i need to tell XJC to create that class? In my point of view there need to be a wrapper class generated!?

Thanks


回答1:


Options:

  • Check your ObjectFactory for a method like createProducts(...).
  • Use JAXBElement<Products>.
  • Customize your element with <jaxb:class name="ProductsElement"/> - you'll get a ProductsElement with @XmlRootElement.
  • You can also use jaxb2-annotate-plugin to add @XmlRootElement to your existing Products class.

Update

Here's a small example from one of my projects. There I have a consrtuct like

<element name="Capabilities" type="wps:WPSCapabilitiesType">
</element>

In the ObjectFactory I have:

@XmlElementDecl(namespace = "http://www.opengis.net/wps/1.0.0", name = "Capabilities")
public JAXBElement<WPSCapabilitiesType> createCapabilities(WPSCapabilitiesType value) {
   return new JAXBElement<WPSCapabilitiesType>(_Capabilities_QNAME, WPSCapabilitiesType.class, null, value);
}

So you should get a method like createProducts(...) in your ObjectFactory - not for the type but for the element. This was about the option 1.

Option 2 - it's not cryptic. You just create an instance of JAXBElement providing a qualified name of the element, type of the value and the value:

new JAXBElement<WPSCapabilitiesType>(_Capabilities_QNAME, WPSCapabilitiesType.class, null, value);

In your case will be something like new JAXBElement<ProductsType>(new QName("products"), Products.class, null, products).

Finally, you say that you have no Products class but only Product class. Hm. This would mean you don't get a class generated for the anonymous complex type which is declared in the products element. Which is not impossible, but I somehow doubt this is a case here. Check your classes if you have a class like ProductsType or ProductsElement.



来源:https://stackoverflow.com/questions/27549171/wrapper-class-missing-when-using-xjc-on-xsd

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