我正在使用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”。我使用绑定错了吗?
答案 0 :(得分:27)
如果要将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);
}
}
<强>实施例强>