枚举与模式不匹配:jaxb 或 xsd 有问题? [英] Enums don't match schema: problem with jaxb or xsd?

查看:31
本文介绍了枚举与模式不匹配:jaxb 或 xsd 有问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to use JAXB to unmarshal this file into Java objects. I know that there's a problem with SAX in J6 that rejects the maxOccurs line, and I've changed it to unbounded. However, when I xjc it, it's not creating all the classes & enums I need. For example, there should be a educationLevelType enum. What's more, I'ved tried MS's xsd unmarshaller, it it creates everything correctly.

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

Can someone with more experience than I look at this and tell me what I'm missing? Is there something that needs to be corrected in the xsd, or is there a bug in JAXB?

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

Update Blaise completely answered this question as asked. Unfortunately, IMHO, this makes JAXB worthless. The whole idea is that I can generate classes from a schema - I shouldn't have to know stuff about the structure beforehand. If I have to create a custom bindings file, I might as well just create a schema that produces the code I want. But then, why stop there? Why not just skip all those steps and generate the classes I want?

最后,一位同事向我指出 Apache XMLBeans - 该项目有点老,但它创建对象没有麻烦.Codehaus 也有一个 xmlbeans-maven-plugin .

In the end, a coworker pointed me to Apache XMLBeans - the project's a little older, but it creates the objects without trouble. Codehaus also has a xmlbeans-maven-plugin for it.

推荐答案

有几个枚举值导致了这个问题.这些问题可以通过使用 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);
    }

}

ma​​xOccurs 问题

对于 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 或 xsd 有问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆