2012-09-24 52 views
6

我們有這樣的聲明的XSD架構:XSD:生成到列表自定義類型的列表<String>

<xsd:simpleType name="customId"> 
    <xsd:annotation> 
     <xsd:appinfo> 
      <jaxb:javaType name="com.company.identifiers.CustomId" parseMethod="fromString" printMethod="toString"/> 
     </xsd:appinfo> 
    </xsd:annotation> 
    <xsd:restriction base="xsd:int" /> 
</xsd:simpleType> 

然後,我想有一個生成的Java類這種類型的列表:

<xsd:complexType name="SomeMessage"> 
    ... 
<xsd:attribute name="customIds" use="optional"> 
     <xsd:simpleType> 
      <xsd:list itemType="customId" /> 
     </xsd:simpleType> 
</xsd:attribute> 
    ... 
</xsd:complexType> 

但是由於某種原因,字段customIds生成爲List<String>

我想,xsd:sequence可以用來代替xsd:list,但SomeMessage已經有一個xsd:choice,並且據我得到的,它是非法的xsd:sequence在相同的聲明。

謝謝!

回答

3

使用在Java 1.7.0_02上運行的NetBeans 7.1.2生成的代碼。

如果要映射簡單類型的Java類,其中一個辦法就是設置全局mapSimpleTypeDef="true"

<xsd:annotation> 
    <xsd:appinfo> 
     <jaxb:globalBindings mapSimpleTypeDef="true"/> 
    </xsd:appinfo> 
</xsd:annotation> 

生成的代碼:

@XmlAccessorType(XmlAccessType.FIELD) 
@XmlType(name = "SomeMessage") 
public class SomeMessage { 

    @XmlAttribute(name = "customIds") 
    protected List<CustomId> customIds; 

    /** 
    * Gets the value of the customIds property. 
    * 
    * <p> 
    * Objects of the following type(s) are allowed in the list 
    * {@link CustomId } 
    * 
    * 
    */ 
    public List<CustomId> getCustomIds() { 
     if (customIds == null) { 
      customIds = new ArrayList<CustomId>(); 
     } 
     return this.customIds; 
    } 

} 

如果你想引用預先存在的CustomId類,那麼下面的作品在我的情況下:

<xsd:simpleType name="customId"> 
    <xsd:restriction base="xsd:int"/> 
</xsd:simpleType> 
<xsd:complexType name="SomeMessage"> 
    <xsd:attribute name="customIds" use="optional"> 
     <xsd:simpleType> 
      <xsd:annotation> 
       <xsd:appinfo> 
        <jaxb:javaType name="java.util.List&lt;com.company.identifiers.CustomId>" parseMethod="Class1.fromString" printMethod="Class1.toString"/> 
       </xsd:appinfo> 
      </xsd:annotation> 
      <xsd:list itemType="customId"/> 
     </xsd:simpleType> 
    </xsd:attribute> 
</xsd:complexType> 

你會得到foll因爲:

@XmlAccessorType(XmlAccessType.FIELD) 
@XmlType(name = "SomeMessage") 
public class SomeMessage { 

    @XmlAttribute(name = "customIds") 
    @XmlJavaTypeAdapter(Adapter1 .class) 
    protected List<CustomId> customIds; 

    /** 
    * Gets the value of the customIds property. 
    * 
    * @return 
    *  possible object is 
    *  {@link String } 
    *  
    */ 
    public List<CustomId> getCustomIds() { 
     return customIds; 
    } 

    /** 
    * Sets the value of the customIds property. 
    * 
    * @param value 
    *  allowed object is 
    *  {@link String } 
    *  
    */ 
    public void setCustomIds(List<CustomId> value) { 
     this.customIds = value; 
    } 

} 

而產生的適配器1:

public class Adapter1 
    extends XmlAdapter<String, List<CustomId>> 
{ 


    public List<CustomId> unmarshal(String value) { 
     return (Class1.fromString(value)); 
    } 

    public String marshal(List<CustomId> value) { 
     return (Class1.toString(value)); 
    } 

} 
+0

謝謝您的回答!實際上,我試圖獲得不是'List ',而是'列表'。我正在使用JAXB 2.2。 – gregvonbautt

+0

至於'xsd:sequence'和'xsd:choice',我的意思是: ' ... ' 對於這種聲明,它表示」元素序列「無效,放錯位置,或者頻繁發生。 – gregvonbautt

+0

我已更新以處理您的說明。要有選擇和序列,您需要像這樣的序列封裝: ... ...。屬性不能放置在模型組(序列/選擇/全部)內,它們居於其外部。如果你想使用元素而不是基於xsd:list的屬性,那麼你可以在我的例子中使用嵌套序列來實現它。 –