简体   繁体   English

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. 这个问题已被各种各样的问题提出来 - 但我觉得仍然有进一步编目的空间。

I have an xsd with two element definitions 我有一个带有两个元素定义的xsd

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

This generates: 这会产生:

protected String date;
@XmlElementRef(name = "lastXdigits", namespace = "http://xxxxxxx", type = JAXBElement.class)
protected JAXBElement<String> lastXDigits;

Changing the xsd to: 将xsd更改为:

<xs:element name="lastXdigits" type="xs:string" nillable="true" minOccurs="1"/>

results in: 结果是:

protected String date;
@XmlElement(name = "lastXdigits", required = true, nillable = true)
protected String lastXDigits;

and using: 和使用:

<xs:element name="lastXdigits" type="xs:string" minOccurs="0"/>

results in: 结果是:

protected String date;
@XmlElement(name = "lastXdigits")
protected String lastXDigits;

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? 为什么在第一种情况下将lastXDigits生成为JAXBElement类型,为什么String类型在所有情况下都不够用? Also, why should jaxb treat these two elements differently when their definition apart from name are identical? 另外,当jaxb除了名称之外的定义相同时,为什么jaxb应该区别对待这两个元素呢?

I'm using a jaxb-xjc ant task from jaxb 2.0.5. 我正在使用jaxb 2.0.5中的jaxb-xjc ant任务。

Does this look suspicious or is there good justification for these differences? 这看起来是否可疑或是否有充分的理由来解决这些差异?

If nillable="true" together with minOccurs="0" it is impossible to represent the values with just a String . 如果nillable="true"minOccurs="0" ,则不可能只用String表示值。 What would null mean? null意味着什么? Does it mean that it was null, or that it occurred 0 times? 这是否意味着它是空的,或者它发生了0次? 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. 空列表肯定与空列表不同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM