xml - JAXB 绑定(bind) - 将 typesafeEnumBase 设置为空(将枚举转换为字符串)

标签 xml xsd jaxb xjc jaxb2-maven-plugin

根据link

If typesafeEnumBase is set to xsd:string, it would be a global way to specify that all simple type definitions deriving directly or indirectly from xsd:string and having enumeration facets should be bound by default to a typesafe enum. If typesafeEnumBase is set to an empty string, "", no simple type definitions would ever be bound to a typesafe enum class by default. The value of typesafeEnumBase can be any atomic simple type definition except xsd:boolean and both binary types.

所以我已经为我的 binding.xjb 设置了以下内容:

<jxb:globalBindings typesafeEnumBase=""/>

并且在运行 jaxb2-maven-plugin 时出现以下异常:

lineNumber: 5; columnNumber: 46; cvc-minLength-valid: Value '' with length = '0' is not facet-valid with respect to minLength '1' for type '#AnonType_typesafeEnumBaseglobalBindings'.
...
lineNumber: 5; columnNumber: 46; cvc-attribute.3: The value '' of attribute 'typesafeEnumBase' on element 'jxb:globalBindings' is not valid with respect to its type, 'null'.

据我所知,我不能将空字符串 "" 设置为 typesafeEnumBase,即使文档是这样说的。文档还提到它不能是 xsd:boolean

我只想将以下内容转换为 String 而不是 enum

<xs:simpleType name="phraseID">
    <xs:restriction base="escapedStringUserType">
        <xs:enumeration value="NOT_SPECIFIED"/>
        <xs:enumeration value="X000-9999"/>
        <xs:enumeration value="X000-9998"/>
    </xs:restriction>
</xs:simpleType>

相关的 SO 问题是 here但由于我无法设置空字符串 "" 或将 xsd:boolean 值设置为 typesafeEnumBase 没有一个答案对我有用(都试过了).

最佳答案

设置如下

<jxb:globalBindings typesafeEnumMaxMembers="0"/>

不会为所有具有枚举限制的简单类型生成任何枚举,而是会将它们转换为字符串,但它会在控制台中发出警告,如下所示:

Simple type "xxx-address" was not mapped to Enum due to EnumMemberSizeCap limit. Facets count: 10, current limit: 0. You can use customization attribute "typesafeEnumMaxMembers" to extend the limit.

关于xml - JAXB 绑定(bind) - 将 typesafeEnumBase 设置为空(将枚举转换为字符串),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46927872/

相关文章:

javascript - XML 声明中的 NativeScript 全局事件处理程序?

c++ - 使用 C++/MSXML 中的变音符号根据嵌入式 XSD 资源验证 XML 文件

java - 有没有办法在 JAXB 中配置渲染深度?

xml - JAXB - 解码时的 XSD 验证不会因缺少属性而失败

gradle - java.lang.NoClassDefFoundError : javax/xml/bind/JAXBException

java.lang.IllegalArgumentException : No view found for id 0x7f090047 ("project name":id/content) for fragment FmMenu 异常

css - 使用 CSS 仅显示内部 XML 标签中的文本

sql - 如何从 MS SQL 2014 中的 SQL 查询生成 JSON

c# - 如何将 XML 读入与其 xsd 匹配的类中

sql - XML Schema totalDigits/fractionDigits 与 SQL precision/scale