枚举与架构 : problem with jaxb or xsd? 不匹配

标签 enums jaxb unmarshalling

我正在尝试使用 JAXB 解码 this file到 Java 对象中。我知道 J6 中的 SAX 存在拒绝 maxOccurs 行的问题,我已将其更改为 unbounded .但是,当我 xjc它,它并没有创建我需要的所有类和枚举。例如,应该有 educationLevelType枚举。更重要的是,我尝试过 MS 的 xsd unmarshaller,它可以正确创建所有内容。

比我更有经验的人可以看看这个并告诉我我错过了什么吗? xsd 中是否有需要更正的地方,或者 JAXB 中是否存在错误?

更新
Blaise 完全按照要求回答了这个问题。不幸的是,恕我直言,这使得 JAXB 一文不值。整个想法是我可以从模式生成类——我不应该事先了解有关结构的东西。如果我必须创建一个自定义绑定(bind)文件,我不妨创建一个生成我想要的代码的模式。但是,为什么要停在那里?为什么不跳过所有这些步骤并生成我想要的类?

最后,一位同事将我指向Apache XMLBeans - 该项目有点旧,但它可以轻松创建对象。 Codehaus 还有一个 xmlbeans-maven-plugin为了它。

最佳答案

有几个枚举值会导致此问题。这些问题可以通过使用 JAXB 外部绑定(bind)文件来解决(见下文)。

枚举问题 #1 - 空字符串

您的一些枚举值是空字符串(“”),这会导致生成字符串而不是枚举属性:

<xs:enumeration value="">
    <xs:annotation>
        <xs:documentation>Blank</xs:documentation> 
    </xs:annotation>
</xs:enumeration>

枚举问题 #2 - 数字字符串

一些枚举值是导致生成字符串而不是枚举属性的数字:
<xs:enumeration value="6">
    <xs:annotation>
        <xs:documentation>6th grade</xs:documentation> 
   </xs:annotation>
</xs:enumeration>

绑定(bind)文件 (bindings.xml)

以下绑定(bind)文件可用于解决educationLevelType 的问题,此处的概念可应用于所有有问题的类型:
<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 标志如下所述):
xjc -nv -b bindings.xml -d out http://www.acf.hhs.gov/programs/cb/systems/nytd/nytd_data_file_format.xsd

这将导致生成以下枚举:
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 模式:
xjc -nv -d out http://www.acf.hhs.gov/programs/cb/systems/nytd/nytd_data_file_format.xsd

这将使您摆脱以下错误,而无需修改 XML 架构:

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

Failed to parse a schema.



更多信息
  • http://blog.bdoughan.com/2011/08/jaxb-and-enums.html
  • 关于枚举与架构 : problem with jaxb or xsd? 不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4820113/

    相关文章:

    java - 在 jaxb 2.0 中使用什么代替 validator

    json - 当指定 UTF-8 编码时,MOXy JAXB 会为 Unicode (u+2019) 编码(marshal)无效的控制字符

    mongodb - 防止 mgo/bson Unmarshal 清除未导出的字段

    c++ - 序列化 C++ 对象

    delphi - 如何更改我的代码以获取正确的枚举名称值?

    ios - 确定关联枚举中的哪个枚举

    typescript 枚举作为参数类型允许无效值

    java - EclipseLink MOXy 存储用 @XmlElement 注释的对象类型字段的 DOM 元素实例

    json - 在 golang 中解码一个 json 数组

    python - 具有无限/动态成员的枚举