我们有一个带有如下声明的 xsd 模式:
<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
是非法的在同一声明中。
谢谢!
最佳答案
使用 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));
}
}
关于java - xsd: 自定义类型的列表生成到 List<String>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12564004/