Java xsd:自定义类型的列表生成到列表<;字符串>;
我们有一个xsd模式,其声明如下:Java xsd:自定义类型的列表生成到列表<;字符串>;,java,xsd,jaxb,Java,Xsd,Jaxb,我们有一个xsd模式,其声明如下: <xsd:simpleType name="customId"> <xsd:annotation> <xsd:appinfo> <jaxb:javaType name="com.company.identifiers.CustomId" parseMethod="fromString" printMethod="toString"/> </xs
<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>
...
...
但是由于某种原因,字段customid
被生成为List
我想,xsd:sequence
可以用来代替xsd:list
,但是SomeMessage
已经有了一个xsd:choice
,据我所知,在同一声明中使用xsd:sequence
是非法的
谢谢 使用运行在Java 1.7.0_02上的NetBeans 7.1.2生成的代码 如果要将简单类型映射到Java类,一种方法是全局设置
mapSimpleTypeDef=“true”
生成的代码:
@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;
}
}
@xmlacessortype(xmlacesstype.FIELD)
@XmlType(name=“SomeMessage”)
公共类消息{
@xmltattribute(name=“customIds”)
受保护的客户ID列表;
/**
*获取CustomId属性的值。
*
*
*列表中允许以下类型的对象
*{@link CustomId}
*
*
*/
公共列表getCustomId(){
if(customIds==null){
customIds=newarraylist();
}
返回此.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;
}
}
@xmlacessortype(xmlacesstype.FIELD)
@XmlType(name=“SomeMessage”)
公共类消息{
@xmltattribute(name=“customIds”)
@XmlJavaTypeAdapter(Adapter1.class)
受保护的客户ID列表;
/**
*获取CustomId属性的值。
*
*@返回
*可能的对象是
*{@link String}
*
*/
公共列表getCustomId(){
返回customid;
}
/**
*设置CustomId属性的值。
*
*@param值
*允许的对象是
*{@link String}
*
*/
public void setCustomId(列表值){
this.customIds=值;
}
}
以及生成的适配器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));
}
}
公共类适配器1
扩展XmlAdapter
{
公共列表解组(字符串值){
返回值(Class1.fromString(value));
}
公共字符串封送处理(列表值){
返回(Class1.toString(值));
}
}
谢谢您的回答!实际上,我想得到的不是列表
,而是列表
。我正在使用JAXB2.2。至于xsd:sequence
和xsd:choice
,我的意思是:代码>对于这种声明,它说“元素‘序列’无效、错位或出现得太频繁。”。我已经更新以处理您的澄清。要有一个选择和一个序列,您需要这样一个序列包装器:。。。。。。。属性不能放置在模型组(序列/选择/全部)内,它们位于模型组外。如果您想使用元素而不是基于xsd:list的属性,那么您可以使用我的示例中的嵌套序列来实现它。再次感谢您!具有完全限定类型的解决方案“java.util.Listcom.company.identifiers.CustomId>”
解决了我的问题。现在我有了列表
。
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));
}
}