如何使用 JAXB 从 XML Schema 生成 Java 枚举?
我正在使用 maven 插件 maven-jaxb2-plugin 从 XSD Schema 文件生成 POJO.这工作正常.唯一真正困扰我的是,xml 模式枚举没有映射到 Java 枚举类型中.
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.
我的 maven 插件正在从我称为 schemachooser.xsd 的文件中生成 java pojos
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.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".我是不是用错了绑定?
It does generate the files, but not the "new" Enum Class "MyEnumType". Am I using the bindings wrong?
推荐答案
如果您想将 JAXB 注释与 XML 模式分开,那么您需要使用 JAXB 绑定文件:
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
以下是根据您的问题进行逆向工程的示例 XML 架构:
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 通话
xjc -extension -d out -b bindings.xml myNormalSchema.xsd
MyEnumType
其中一个生成的类是一个名为 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 类也是用 isSet
方法生成的:
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);
}
}
示例
- http://bdoughan.blogspot.com/2011/05/schema-to-java-xmlmimetype.html
- http://bdoughan.blogspot.com/2011/04/xml-schema-to-java-xsd-choice.html
相关文章