开发者

Question about Jaxb 2.x SchemaGen

开发者 https://www.devze.com 2023-01-29 01:28 出处:网络
i am trying to generate schema using jaxb from my exisitng POJO classes and till now its working fine now i have a require开发者_如何转开发ment where i need to declare a attribute type is my XSD but t

i am trying to generate schema using jaxb from my exisitng POJO classes and till now its working fine now i have a require开发者_如何转开发ment 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


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!

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号