问题描述
我正在使用maven插件maven-jaxb2-plugin从XSD Schema文件生成POJO。
这工作正常。唯一真正困扰我的是,xml架构枚举没有映射到Java Enum Type中。
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插件是从一个文件生成java pojos我打电话给schemachooser.xsd
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);
}
}
根
此外,根类生成与 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
这篇关于如何使用JAXB从XML架构生成Java Enum?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!