在xsd上使用xjc时,包装类丢失 [英] wrapper class missing when using xjc on xsd
问题描述
我有以下XSD:
<xsd:element name="products" >
<xsd:complexType>
<xsd:sequence>
<xsd:element name="product" type="foo:myProduct" maxOccurs="unbounded" />
</xsd:sequence>
</xsd:complexType>
</xsd:element>
现在当我发行XJC时,它不会生成Products.class文件,只会生成Product.class。但当然我的XML看起来像这样:
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>
所以最后,我没有一个带有XmlRootElement注释的类,这是奇怪的。当然我不能让编组工作。
So at the end, i dont have a class with XmlRootElement annotation, which is odd. Of course i cant get marshalling working.
任何暗示我的XSD可能出错的地方或者我需要告诉XJC创建该类?在我看来,需要生成一个包装类!?
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!?
谢谢
推荐答案
选项:
- 检查
ObjectFactory
以查找类似的方法createProducts(...)
。 - 使用
JAXBElement< Products>
。 - 使用
< jaxb:class name =ProductsElement/>
自定义元素 - 您将获得ProductsElement
with@XmlRootElement
。 - 您还可以使用 jaxb2-annotate-plugin 添加
@XmlRootElement
到你现有的产品
类。
- 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.
更新
这是我的一个项目中的一个小例子。我有一个类似
Here's a small example from one of my projects. There I have a consrtuct like
<element name="Capabilities" type="wps:WPSCapabilitiesType">
</element>
在 ObjectFactory
中我有:
@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);
}
所以你应该得到像 createProducts(。你的
- 不是类型而是元素。这是关于选项1。 ObjectFactory
中的..)
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.
选项2 - 它并不神秘。您只需创建一个 JAXBElement
的实例,提供元素的限定名称,值的类型和值:
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);
在你的情况下将是类似的新JAXBElement< ProductsType>(新的QName (产品),Products.class,null,产品)
。
In your case will be something like new JAXBElement<ProductsType>(new QName("products"), Products.class, null, products)
.
最后,你说你没有产品
类,但只有产品
类。嗯。这意味着您不会获得为 products
元素中声明的匿名复杂类型生成的类。这不是不可能的,但我怀疑这是一个案例。如果您有类似 ProductsType
或 ProductsElement
,请检查您的课程。
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
.
这篇关于在xsd上使用xjc时,包装类丢失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!