2010-07-02 43 views
2

因此,我一直在阅读关于在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一起使用?

+0

你是怎么解决这个问题?我遇到了一个类似的问题 - 我有一个用'type =“xs:IDREF”maxoccurs =“unbounded”声明的元素,并且用基类型注解,并且类在Java类中被正确声明,但是在运行时它是一个List >代替。 – undefined 2013-05-01 16:11:16

回答

0

XJC使用createSimViewTypeITEM方法生成ObjectFactory来创建ITEM元素。

/** 
* Create an instance of {@link JAXBElement }{@code <}{@link Object }{@code >}} 
* 
*/ 
@XmlElementDecl(namespace = "", name = "ITEM", scope = SimViewType.class) 
@XmlIDREF 
public JAXBElement<Object> createSimViewTypeITEM(Object value) { 
    return new JAXBElement<Object>(_SimViewTypeITEM_QNAME, Object.class, SimViewType.class, value); 
} 

它有帮助吗?

0

你可以注释您的模式如下,以获得所需等级:

<xsd:schema 
    xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
    xmlns="http://www.mycompany.org/exchange" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" 
    targetNamespace="http://www.mycompany.org/exchange" 
    elementFormDefault="qualified" attributeFormDefault="unqualified" 
    jaxb:version="2.1"> 

    <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:annotation> 
         <xsd:appinfo> 
          <jaxb:property> 
           <jaxb:baseType name="org.mycompany.exchange.CustomerType"/> 
          </jaxb:property> 
         </xsd:appinfo> 
        </xsd:annotation> 
       </xsd:element> 
      </xsd:choice> 
      <!--xsd:element name="items" type="ItemType" maxOccurs="unbounded"/--> 
     </xsd:sequence> 
    </xsd:complexType> 

</xsd:schema> 

以下是结果类:

package org.mycompany.exchange; 

import javax.xml.bind.annotation.XmlAccessType; 
import javax.xml.bind.annotation.XmlAccessorType; 
import javax.xml.bind.annotation.XmlElement; 
import javax.xml.bind.annotation.XmlIDREF; 
import javax.xml.bind.annotation.XmlSchemaType; 
import javax.xml.bind.annotation.XmlType; 


@XmlAccessorType(XmlAccessType.FIELD) 
@XmlType(name = "OrderType", propOrder = { 
    "customer", 
    "custref" 
}) 
public class OrderType { 

    protected org.mycompany.exchange.CustomerType customer; 
    @XmlElement(type = Object.class) 
    @XmlIDREF 
    @XmlSchemaType(name = "IDREF") 
    protected org.mycompany.exchange.CustomerType custref; 

    public org.mycompany.exchange.CustomerType getCustomer() { 
     return customer; 
    } 

    public void setCustomer(org.mycompany.exchange.CustomerType value) { 
     this.customer = value; 
    } 

    public org.mycompany.exchange.CustomerType getCustref() { 
     return custref; 
    } 

    public void setCustref(org.mycompany.exchange.CustomerType value) { 
     this.custref = value; 
    } 

}