问题
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 likecreateProducts(...)
. - Use
JAXBElement<Products>
. - Customize your element with
<jaxb:class name="ProductsElement"/>
- you'll get aProductsElement
with@XmlRootElement
. - You can also use jaxb2-annotate-plugin to add
@XmlRootElement
to your existingProducts
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