因此,我一直在阅读关于在JAXB here中使用ID和IDREF的示例,并且在所示示例中它们使用了1个IDREF元素,它在生成的Java中显示为Object码。我同时包括XML Schema ...1个IDREF和多于1个JAXB不一致之处
<xsd:complexType name="CustomerType">
<xsd:sequence>
<xsd:element name="id" type="xsd:ID"/>
<xsd:element name="name" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="OrderType">
<xsd:sequence>
<xsd:choice>
<xsd:element name="customer" type="CustomerType"/>
<xsd:element name="custref" type="xsd:IDREF"/>
</xsd:choice>
<xsd:element name="items" type="ItemType" maxOccurs="unbounded"/>
</xsd:sequence>
</xsd:complexType>
和后代的Java代码。
public class OrderType {
protected CustomerType customer;
protected Object custref;
protected List items;
public CustomerType getCustomer() {
return customer;
}
public void setCustomer(CustomerType value) {
this.customer = value;
}
public Object getCustref() {
return custref;
}
public void setCustref(Object value) {
this.custref = value;
}
public List getItems() {
if (items == null) {
items = new ArrayList();
}
return this.items;
}
}
现在,在我的代码,我试过的东西一点点不同,由下列模式例证:
<xsd:complexType name="SimViewType">
<xsd:sequence>
<xsd:element name="NAME" type="xsd:string"></xsd:element>
<xsd:element name="ITEM" type="am:SimItemIDREF" minOccurs="0" maxOccurs="unbounded"></xsd:element>
</xsd:sequence>
<xsd:attribute name="id" type="xsd:ID" use="required"></xsd:attribute>
</xsd:complexType>
<xsd:simpleType name="SimItemIDREF">
<xsd:restriction base="xsd:IDREF"></xsd:restriction>
</xsd:simpleType>
虽然我包围的一般IDREF有SimItemIDREF,它只是可读性。然而,即使我用一个正常的xsd:IDREF相反,一个简单的事实我用1以外的值意味着我的Java代码结束了下面......
public class SimViewType {
...
protected List<JAXBElement<Object>> item;
...
}
一个List<Object>
的相反,我得到一个List<JAXElement<Object>>
。现在,如果我想创建一个JAXElement,我可以做得很好,但是我不能将它添加到我的SimViewType中,因为JAXElement<SimItemType>
和JAXElement<Object>
之间没有转换。 SimViews实际上并不保留对SimItems的控制的想法在我的设计中至关重要,所以仅仅删除引用并不是一个好主意,因为它可能会在运行时造成危险的差异。
如何创建SimItemType或JAXBElement,以便我可以将其与SimViewType一起使用?
你是怎么解决这个问题?我遇到了一个类似的问题 - 我有一个用'type =“xs:IDREF”maxoccurs =“unbounded”声明的元素,并且用基类型注解,并且类在Java类中被正确声明,但是在运行时它是一个List>代替。 –
undefined
2013-05-01 16:11:16