简体   繁体   English

JAXB何时生成@XmlElementRef和@XmlElement类型的注释?

[英]When does JAXB generates @XmlElementRef and @XmlElement type of annotations?

This question has been asked a multiple times in different ways, but I have a query from a different perspective here. 这个问题已经以不同的方式被多次询问,但我在这里从不同的角度进行查询。 So let's take below example : 让我们以下面的例子:

<xs:element name="Ric" type="String" nillable="true" minOccurs="0"/>

For above line in XSD the JAXB generated below code : 对于XSD中的上一行,JAXB生成如下代码:

@XmlElementRef(name = "Ric", type = JAXBElement.class, required = false)
    protected JAXBElement<String> ric;

but for following xsd element, 但是对于跟随xsd元素,

<xs:element name="Ric" type="String" minOccurs="0"/>

I get, 我明白了

 @XmlElement(name = "Ric")
    protected String ric;

I have read several explanations of the difference between @XmlElement vs @XmlElementRef but since I am very new to working with XSDs I could not understand what they are trying to say. 我已经阅读了几个关于@XmlElement@XmlElementRef之间差异的解释,但由于我对使用XSD非常新,我无法理解他们想说的是什么。 All I could grasp was that for nillable="true" this is the case. 我所能掌握的是,对于nillable="true" ,情况就是如此。 Can someone please explain in a layman's language that why JAXB generated a parameter type for an element which has nillable="true" and minOccur="0" . 有人可以用外行的语言解释为什么JAXB为一个元素生成一个参数类型,该元素具有nillable="true"minOccur="0" I know we can do away with just one of these too, but client needs it this way. 我知道我们也可以取消其中一个,但客户需要这样做。

jaxb version : 2.2.11 jaxb版本:2.2.11

JAXB is all about mapping between Java and XML, in both directions. JAXB是关于Java和XML之间两个方向的映射。

So for a schema of: 因此对于以下模式:

<xs:element name="Ric" type="String" minOccurs="0"/>

a Java like: 像Java一样:

protected String ric;  

with ric = null unambiguously maps to the absence of the "Ric" element (ie the parent element has zero "Ric" children elements). with ric = null明确地映射到缺少“Ric”元素(即父元素具有零“Ric”子元素)。

But for a schema of: 但对于以下架构:

<xs:element name="Ric" type="String" nillable="true" minOccurs="0"/>

you wouldn't know whether a String equal to 'null' maps to zero "Ric" elements or a "Ric" element with value xsi:nil . 你不知道一个等于'null'的字符串是映射到零“Ric”元素还是一个值为xsi:nil的“Ric”元素。 So you need the extra JAXBElement wrapper to differentiate. 所以你需要额外的JAXBElement包装来区分。

See the accepted answer here . 请在此处查看接受的答案。

Not sure my answer is any more easy to understand than that.. but maybe it helped. 不确定我的答案是否比那更容易理解..但也许它有所帮助。

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

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