带有数值的 JAXB 枚举 [英] JAXB enumeration with numeric values
问题描述
我想限制这样的输入值
<simpleType name="SomeCode">
<restriction base="string">
<enumeration value="036222B"/>
<enumeration value="036111C"/>
</restriction>
</simpleType>
但这不会生成枚举.我怀疑这是因为值以数字开头,而 Enum 值不允许这样做.
But this does not generate an Enum. I suspect it is because the values start with numbers and this is not allowed for Enum values.
是否有任何解决方案或解决方法?
Is there any solution or workaround?
推荐答案
这是我对一个类似问题的回答,可能会有所帮助(请参阅问题 2):
Here is my answer to a similar question that may help (see issue 2):
有几个枚举值会导致此问题.这些问题可以通过使用 JAXB 外部绑定文件来克服(见下文).
There are a couple of enumeration values that are causing this issue. These issues can be overcome through the use of a JAXB external binding file (see below).
枚举问题 #1 - 空字符串
您的某些枚举值是空字符串 (""),这会导致生成字符串而不是枚举属性:
Some of your enum values are empty string (""), which is causing a String rather than an enum property to be generated:
<xs:enumeration value="">
<xs:annotation>
<xs:documentation>Blank</xs:documentation>
</xs:annotation>
</xs:enumeration>
枚举问题 #2 - 数字字符串
一些枚举值是导致生成字符串而不是枚举属性的数字:
Some of the enum values are numbers which is causing a String rather than an enum property to be generated:
<xs:enumeration value="6">
<xs:annotation>
<xs:documentation>6th grade</xs:documentation>
</xs:annotation>
</xs:enumeration>
绑定文件(bindings.xml)
以下绑定文件可用于解决educationLevelType的问题,这里的概念可以应用于所有有问题的类型:
The following bindings file can be used to address the issues with the educationLevelType, the concepts here can be applied to all the problematic types:
<jxb:bindings
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:jxb="http://java.sun.com/xml/ns/jaxb"
version="2.1">
<jxb:bindings schemaLocation="http://www.acf.hhs.gov/programs/cb/systems/nytd/nytd_data_file_format.xsd">
<jxb:bindings node="//xs:simpleType[@name='educationLevelType']/xs:restriction/xs:enumeration[@value='6']">
<jxb:typesafeEnumMember name="SIX"/>
</jxb:bindings>
<jxb:bindings node="//xs:simpleType[@name='educationLevelType']/xs:restriction/xs:enumeration[@value='7']">
<jxb:typesafeEnumMember name="SEVEN"/>
</jxb:bindings>
<jxb:bindings node="//xs:simpleType[@name='educationLevelType']/xs:restriction/xs:enumeration[@value='8']">
<jxb:typesafeEnumMember name="EIGHT"/>
</jxb:bindings>
<jxb:bindings node="//xs:simpleType[@name='educationLevelType']/xs:restriction/xs:enumeration[@value='9']">
<jxb:typesafeEnumMember name="NINE"/>
</jxb:bindings>
<jxb:bindings node="//xs:simpleType[@name='educationLevelType']/xs:restriction/xs:enumeration[@value='10']">
<jxb:typesafeEnumMember name="TEN"/>
</jxb:bindings>
<jxb:bindings node="//xs:simpleType[@name='educationLevelType']/xs:restriction/xs:enumeration[@value='11']">
<jxb:typesafeEnumMember name="ELEVEN"/>
</jxb:bindings>
<jxb:bindings node="//xs:simpleType[@name='educationLevelType']/xs:restriction/xs:enumeration[@value='12']">
<jxb:typesafeEnumMember name="TWELVE"/>
</jxb:bindings>
<jxb:bindings node="//xs:simpleType[@name='educationLevelType']/xs:restriction/xs:enumeration[@value='']">
<jxb:typesafeEnumMember name="BLANK"/>
</jxb:bindings>
</jxb:bindings>
</jxb:bindings>
XJC 调用可以如下进行(-nv 标志如下所述):
The XJC call can be made as follows (the -nv flag is described below):
xjc -nv -b bindings.xml -d out http://www.acf.hhs.gov/programs/cb/systems/nytd/nytd_data_file_format.xsd
这将导致生成以下枚举:
This will cause the following Enum to be generated:
package gov.hhs.acf.nytd;
import javax.xml.bind.annotation.XmlEnum;
import javax.xml.bind.annotation.XmlEnumValue;
import javax.xml.bind.annotation.XmlType;
@XmlType(name = "educationLevelType")
@XmlEnum
public enum EducationLevelType {
@XmlEnumValue("under 6")
UNDER_6("under 6"),
@XmlEnumValue("6")
SIX("6"),
@XmlEnumValue("7")
SEVEN("7"),
@XmlEnumValue("8")
EIGHT("8"),
@XmlEnumValue("9")
NINE("9"),
@XmlEnumValue("10")
TEN("10"),
@XmlEnumValue("11")
ELEVEN("11"),
@XmlEnumValue("12")
TWELVE("12"),
@XmlEnumValue("post secondary")
POST_SECONDARY("post secondary"),
@XmlEnumValue("college")
COLLEGE("college"),
@XmlEnumValue("")
BLANK("");
private final String value;
EducationLevelType(String v) {
value = v;
}
public String value() {
return value;
}
public static EducationLevelType fromValue(String v) {
for (EducationLevelType c: EducationLevelType.values()) {
if (c.value.equals(v)) {
return c;
}
}
throw new IllegalArgumentException(v);
}
}
maxOccurs 问题
对于 maxOccurs 问题,以下带有 no verify (-nv) 标志的命令行可用于解析 XML 架构:
For the maxOccurs issue, the following command line with the no verify (-nv) flag can be used to parse the XML schema:
xjc -nv -d out http://www.acf.hhs.gov/programs/cb/systems/nytd/nytd_data_file_format.xsd
这将使您无需修改 XML 架构即可解决以下错误:
This will get you past the following error without having to modify the XML schema:
正在解析架构... [错误] 当前解析器的配置没有允许 maxOccurs 属性值设置为大于值 5,000.
第 41 行http://www.acf.hhs.gov/programs/cb/systems/nytd/nytd_data_file_format.xsd
parsing a schema... [ERROR] Current configuration of the parser doesn't allow a maxOccurs attribute value to be set greater than the value 5,000.
line 41 of http://www.acf.hhs.gov/programs/cb/systems/nytd/nytd_data_file_format.xsd
未能解析架构.
了解更多信息
这篇关于带有数值的 JAXB 枚举的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!