我正在尝试使用 jaxb 从我现有的 POJO 类生成模式,到目前为止它工作正常,现在我有一个要求,我需要声明属性类型是我的 XSD,但属性值应该是预定义值之一。 下面是我类(class)的代码快照
private String destinationID;
private String contactNo;
private String type;
@XmlAttribute
private String 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>
我无法找出我需要在类里面做什么才能实现这种情况
提前致谢
最佳答案
您可以像这样定义枚举:
@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"/>
祝您的项目顺利!
关于java - 关于 Jaxb 2.x SchemaGen 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4354655/