Jaxb生成的类使用JAXBElement而不是指定的类型 [英] Jaxb generated class used JAXBElement instead of specified type
问题描述
这个问题有各种各样的问题 - 但我觉得仍然有进一步编目的空间。
This question has been asked in various guises - but I feel there is still room to catalogue this further.
我有一个带有两个元素定义的xsd
I have an xsd with two element definitions
<xs:complexType name="elementA">
<xs:sequence>
<xs:element name="date" type="xs:string" minOccurs="0"/>
<xs:element name="lastXdigits" type="xs:string" nillable="true" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
这会产生:
protected String date;
@XmlElementRef(name = "lastXdigits", namespace = "http://xxxxxxx", type = JAXBElement.class)
protected JAXBElement<String> lastXDigits;
将xsd更改为:
<xs:element name="lastXdigits" type="xs:string" nillable="true" minOccurs="1"/>
结果为:
protected String date;
@XmlElement(name = "lastXdigits", required = true, nillable = true)
protected String lastXDigits;
并使用:
<xs:element name="lastXdigits" type="xs:string" minOccurs="0"/>
结果为:
protected String date;
@XmlElement(name = "lastXdigits")
protected String lastXDigits;
这对我来说似乎很奇怪。为什么在第一种情况下将lastXDigits生成为JAXBElement类型,为什么String类型在所有情况下都不够用?另外,当jaxb除了名称之外的定义相同时,为什么jaxb应该区别对待这两个元素呢?
This seems extremely odd to me. Why is lastXDigits generated as a JAXBElement type in the first case and why does a String type not suffice in all cases? Also, why should jaxb treat these two elements differently when their definition apart from name are identical?
我正在使用jaxb 2.0.5中的jaxb-xjc ant任务。
I'm using a jaxb-xjc ant task from jaxb 2.0.5.
这看起来多么可疑或者这些差异有充分的理由吗?
Does this look suspicious or is there good justification for these differences?
推荐答案
如果 nillable =true
与 minOccurs =0
一起只能用 String
来表示值。 null
是什么意思?这是否意味着它是空的,或者它发生了0次?但是我想这只是一个字符串有点没有用。
If nillable="true"
together with minOccurs="0"
it is impossible to represent the values with just a String
. What would null
mean? Does it mean that it was null, or that it occurred 0 times? But I guess that is somewhat moot when it comes to just a String.
与空列表比较。空列表肯定与空列表不同。
Compare with empty list. A null list is certainly different to an empty one.
这篇关于Jaxb生成的类使用JAXBElement而不是指定的类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!