使用 NetBeans 7.1.2 生成的代码,在 Java 1.7.0_02 上运行。
如果要将简单类型映射到 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>
您将获得以下信息:
@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;
}
}
以及生成的Adapter1:
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));
}
}