如何使用JAXB从XML Schema生成Java Enum?

我正在使用maven插件maven-jaxb2-plugin从XSD Schema文件生成POJO。 这很好用。 唯一让我困扰的是,xml模式枚举没有映射到Java Enum Type中。

我的maven插件是从我称为schemachooser.xsd的文件生成java pojos

schemachooser.xsd

                

它确实生成文件,但不生成“新”枚举类“MyEnumType”。 我使用绑定错了吗?

如果要将JAXB注释与XML模式分开,则需要使用JAXB绑定文件:

bindings.xml

           

myNormalSchema.xsd

下面是一个示例XML架构,从您的问题反向设计:

                   

XJC电话

 xjc -extension -d out -b bindings.xml myNormalSchema.xsd 

MyEnumType

其中一个生成的类是一个名为MyEnumType的枚举。

 package com.example; import javax.xml.bind.annotation.XmlEnum; import javax.xml.bind.annotation.XmlType; @XmlType(name = "") @XmlEnum public enum MyEnumType { MY_ENUM_1, MY_ENUM_2; public String value() { return name(); } public static MyEnumType fromValue(String v) { return valueOf(v); } } 

Root类也是使用isSet方法生成的:

 package com.example; import java.io.Serializable; import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlRootElement; import javax.xml.bind.annotation.XmlType; @XmlAccessorType(XmlAccessType.FIELD) @XmlType(name = "", propOrder = { "elementName" }) @XmlRootElement(name = "Root") public class Root implements Serializable { @XmlElement(name = "ElementName", required = true) protected MyEnumType elementName; public MyEnumType getElementName() { return elementName; } public void setElementName(MyEnumType value) { this.elementName = value; } public boolean isSetElementName() { return (this.elementName!= null); } } 

例子