問題描述
所以我想要一個用@XmlElements 注釋的列表,如下所示
So I want to have a list to be annotated with @XmlElements like the following
@XmlElements(
{
@XmlElement(name = "Apple", type = Apple.class),
@XmlElement(name = "Orange", type = Orange.class),
@XmlElement(name = "Mango", type = Mango.class)
}
)
public List<Fruit> getEntries() {
return fruitList;
}
我想知道是否有辦法強制列表包含至少 1 個元素,因為現在,xsd 看起來像
I am wondering whether there is a way to enforce the list to contain at least 1 element, because right now, the xsd looks like
<xs:complexType name="fruitList">
<xs:sequence>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="Apple" type="tns:apple"/>
<xs:element name="Orange" type="tns:orange"/>
<xs:element name="Mango" type="tns:mango"/>
</xs:choice>
</xs:sequence>
</xs:complexType>
推薦答案
假設 Apple、Orange 和 Mango 是 Fruit 的子類,您可能需要使用 @XmlElementRef 注釋
對應于 XML 模式中的替換組,而不是 entries
屬性@XmlElements
對應于選擇的概念.
Assuming that Apple, Orange, and Mango are subclasses of Fruit you may want to annotate the entries
property with @XmlElementRef
which corresponds to substitution groups in XML schema, rather than @XmlElements
which corresponds to the concept of choice.
@XmlElementRef
public List<Fruit> getEntries() {
return fruitList;
}
這假設 Apple、Orange 和 Mango 類擴展了 Fruit 類,并使用 @XmlRootElement
This assumes that the Apple, Orange, and Mango classes extend the Fruit class, and are annotated with @XmlRootElement
@XmlRootElement
public class Apple extends Fruit {
...
}
更多信息
- http://bdoughan.blogspot.com/2010/11/jaxb-and-inheritance-using-substitution.html
- http://bdoughan.blogspot.com/2010/10/jaxb-and-xsd-choice-xmlelements.html
這篇關于JAXB @XmlElements minOccurs = 1的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!