枚举不符合模式:jaxb或xsd的问题? [英] Enums don't match schema: problem with jaxb or xsd?

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

问题描述

我正在尝试使用JAXB来解散此文件转换成Java对象。我知道J6中的SAX有一个问题,它拒绝了maxOccurs行,而且我将其更改为 unbounded 。但是,当我 xjc 它,它不是创建所有的类&我需要的枚举例如,应该有一个 educationLevelType 枚举。更重要的是,我尝试过MS的xsd解组器,它正确地创建了所有的东西。

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完全回答了这个问题。不幸的是,IMHO,这使得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 - 空字符串

您的某些枚举值空字符串(),这将导致生成一个String而不是一个枚举属性:

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 - 数字字符串

某些枚举值是导致生成String而不是枚举属性的数字:

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 Issue

maxOccurs Issue

对于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模式: / p>

This will get you past the following error without having to modify the XML schema:


解析模式... [ERROR]当前
解析器的配置不
允许maxOccurs属性值为
设置为大于值5,000。

第41行
http://www.acf.hhs.gov/programs/cb/systems/nytd/nytd_data_file_format.xsd

无法解析模式。

更多信息

  • http://blog.bdoughan.com/2011/08/jaxb-and-enums.html

这篇关于枚举不符合模式:jaxb或xsd的问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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