使用在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<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));
}
}
謝謝您的回答!實際上,我試圖獲得不是'List',而是'列表'。我正在使用JAXB 2.2。 –
gregvonbautt
至於'xsd:sequence'和'xsd:choice',我的意思是: ' ... xsd:sequence> xsd:complexType>' 對於這種聲明,它表示」元素序列「無效,放錯位置,或者頻繁發生。 –
gregvonbautt
我已更新以處理您的說明。要有選擇和序列,您需要像這樣的序列封裝: ... ... 。屬性不能放置在模型組(序列/選擇/全部)內,它們居於其外部。如果你想使用元素而不是基於xsd:list的屬性,那麼你可以在我的例子中使用嵌套序列來實現它。 –