2012-06-07 14 views
1

爲什麼會這樣架構:爲什麼JAXB會從Schema中的兩個字段中生成單個List <JAXBElement <String>>字段?

<xsd:complexType name="ErrType"> 
<xsd:sequence minOccurs="0" maxOccurs="unbounded"> 
<xsd:element name="errorCode" type="xsd:string"/> 
<xsd:element name="errorDescription" type="xsd:string"/> 
</xsd:sequence> 
</xsd:complexType> 

生成這個Java代碼:

class ErrType { 
    @XmlElementRefs({ 
    @XmlElementRef(name = "errorCode", namespace = "http://somewhere/blah.xsd", type = JAXBElement.class), 
    @XmlElementRef(name = "errorDescription", namespace = "http://somewhere/blah.xsd", type = JAXBElement.class) 
    }) 
    protected List<JAXBElement<String>> errorCodeAndErrorDescription; 
    // ... 
} 

我本來期望更多的東西一樣:

class ErrType extends ArrayList<ErrTypeEntry> {} 
class ErrTypeEntry { 
    protected String errorCode 
    protected String errorDescription; 
} 

好了,所以我想答案是:因爲它。將兩個字段組合成一個字段似乎是非常不可取的。它不必要地刪除了重要的結構。

+0

什麼是生成的代碼的問題?你會期待什麼? – Attila

+1

也許是因爲您在序列元素上指定了minOccurs =「0」maxOccurs =「unbounded」。嘗試在元素中指定它們。 –

回答

1

我的猜測是,你必須寫你的模式更像是這個有點讓你的期望更接近於(結構):

<xsd:complexType name="ErrTypeEntry"> 
    <xsd:sequence> 
    <xsd:element name="errorCode" type="xsd:string"/> 
    <xsd:element name="errorDescription" type="xsd:string"/> 
    </xsd:sequence> 
</xsd:complexType> 

<xsd:complexType name="Errors"> 
    <xsd:sequence> 
    <xsd:element name="error" type="ErrTypeEntry" minOccurs="0" maxOccurs="unbounded"/> 
    </xsd:sequence> 
</xsd:complexType> 
相關問題