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

我使用maven插件maven-jaxb2-plugin从XSD Schema文件生成POJO。
这工作正常唯一真正困扰我的是,xml架构枚举没有映射到Java Enum Type中。

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

schemachooser.xsd:

<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema" xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:sch="http://www.ascc.net/xml/schematron" 
targetNamespace="http://schema.something" elementFormDefault="qualified"
version="1.0" xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
jaxb:extensionBindingPrefixes="xjc" jaxb:version="1.0">

<xs:annotation>
    <xs:appinfo>
        <jaxb:globalBindings generateIsSetMethod="true" fixedAttributeAsConstantProperty="true">
            <xjc:serializable />
        </jaxb:globalBindings>
        <jaxb:schemaBindings>
           <jaxb:bindings node="//xsd:element[@name='ElementName']/xsd:simpleType">
               <jaxb:typesafeEnumClass name="MyEnumType" />
           </jaxb:bindings>
        </jaxb:schemaBindings>
    </xs:appinfo>
</xs:annotation>

<xs:include schemaLocation="myNormalSchema.xsd" />

</schema>

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

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

bindings.xml

<jaxb:bindings 
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
    xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
    jaxb:extensionBindingPrefixes="xjc"
    version="2.1">
    <jaxb:globalBindings generateIsSetMethod="true" fixedAttributeAsConstantProperty="true">
        <xjc:serializable />
    </jaxb:globalBindings>
    <jaxb:bindings schemaLocation="myNormalSchema.xsd">
        <jaxb:bindings node="//xs:element[@name='ElementName']/xs:simpleType">
               <jaxb:typesafeEnumClass name="MyEnumType" />
        </jaxb:bindings>
   </jaxb:bindings>
</jaxb:bindings>

myNormalSchema.xsd

下面是一个从您的问题反向设计的示例XML模式:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema 
    targetNamespace="http://www.example.com" 
     xmlns="http://www.example.com" 
     xmlns:xs="http://www.w3.org/2001/XMLSchema">

    <xs:element name="ElementName">
        <xs:simpleType>
            <xs:restriction base="xs:string">
                <xs:enumeration value="MY_ENUM_1"/>
                <xs:enumeration value="MY_ENUM_2"/>
            </xs:restriction>
        </xs:simpleType>
   </xs:element>

    <xs:element name="Root">
        <xs:complexType>
            <xs:sequence>
                <xs:element ref="ElementName"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>

</xs:schema>

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);
    }

}

例子

> http://bdoughan.blogspot.com/2011/05/schema-to-java-xmlmimetype.html
> http://bdoughan.blogspot.com/2011/04/xml-schema-to-java-xsd-choice.html

翻译自:https://stackoverflow.com/questions/6031572/howto-generate-java-enum-from-xml-schema-with-jaxb

转载注明原文:如何使用JAXB从XML架构生成Java Enum?