开发者

Howto generate Java Enum from XML Schema with JAXB?

I am using the maven plugin maven-jaxb2-plugin to generate POJOs from a XSD Schema file. This works fine. The only thing, thats really bothering me is, that the xml schema enumerations are not mapped in a Java Enum Type.

My maven plugin is generating the java pojos from a file I called schemachooser.xsd

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.so开发者_开发技巧mething" 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>

It does generate the files, but not the "new" Enum Class "MyEnumType". Am I using the bindings wrong?


If you want to keep the JAXB annotations separate from the XML schema then you need to use an JAXB bindings file:

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

Below is a sample XML schema that a reverse engineered from your question:

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

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

MyEnumType

One of the generated classes is an enum called 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

Also the Root class is generated with the isSet method:

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

}

Examples

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜