0

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

Logemann
  • 2,767
  • 33
  • 53

1 Answers1

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

lexicore
  • 42,748
  • 17
  • 132
  • 221
  • the point is, i dont have a Product.class and i want jaxb to create one for me. So option (1) is not feasible because of course there is no generated creator method for products. Since XSD comes from 3rd party and i want to be upgradeable, i dont want to fiddle with the XSD. Option2 is to cryptic for me. – Logemann Dec 18 '14 at 15:09