关于 Jaxb 2.x SchemaGen 的问题

发布于 2024-10-06 15:56:53 字数 771 浏览 1 评论 0原文

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

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>

我无法找出我需要在班级中做什么才能实现这种情况,

提前致谢

i am trying to generate schema using jaxb from my exisitng POJO classes and till now its working fine now i have a requirement where i need to declare a attribute type is my XSD but the attribute value should be one of the predefined values.
below is the code snap shot from my class

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

my requirement is that name should contain any of the predeifned value a schema similar to this

<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>

i am unable to find out what things i need to do in my class in order to achieve this case

thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

治碍 2024-10-13 15:56:53

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

@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"/>

您的项目好运!

You can define an enum like this:

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

and then simply use that in your class:

@XmlAttribute
public ProductReferenceType type;

This will generate XSD as follows:

  <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>

and

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

Good luck in your project!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文