1

我正在尝试使用我现有的 POJO 类中的 jaxb 生成模式,直到现在它工作正常,现在我有一个要求,我需要声明一个属性类型是我的 XSD,但属性值应该是预定义值之一。下面是我班上的代码快照

private String destinationID;
private String contactNo;
private String type;
@XmlAttribute
private String name;

我的要求是 name 应该包含任何预定义的值一个类似于这个的模式

<xsd:attribute name="type"
        type="simpleType.Generic.ProductReferenceType" use="required" />
<xsd:simpleType name="simpleType.Generic.ProductReferenceType">
    <xsd:restriction base="xsd:string">
        <xsd:enumeration value="OFFER" />
        <xsd:enumeration value="SELLER" />
        <xsd:enumeration value="DEFINITION" />
    </xsd:restriction>
</xsd:simpleType>

我无法找出我需要在课堂上做什么才能实现这种情况

提前致谢

4

1 回答 1

2

你可以像这样定义一个枚举:

@XmlType(name="simpleType.Generic.ProductReferenceType")
public enum ProductReferenceType { 
    OFFER,
    SELLER,
    DEFINITION
}

然后在你的课堂上简单地使用它:

@XmlAttribute
public ProductReferenceType type;

这将生成 XSD,如下所示:

  <xs:simpleType name="simpleType.Generic.ProductReferenceType">
    <xs:restriction base="xs:string">
      <xs:enumeration value="OFFER"/>
      <xs:enumeration value="SELLER"/>
      <xs:enumeration value="DEFINITION"/>
    </xs:restriction>
  </xs:simpleType>

    <xs:attribute name="type" type="simpleType.Generic.ProductReferenceType"/>

祝你的项目好运!

于 2011-01-09T13:59:42.067 回答