2012-06-07 15 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> 
相关问题