是否可以使用 JAXB 从模式映射到 java.util.Map? [英] Is it possible to use JAXB to map from schema to a java.util.Map?

查看:26
本文介绍了是否可以使用 JAXB 从模式映射到 java.util.Map?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个现有的 XML 模式,其中包含我想使用 JAXB 映射到某种 Java 映射的类型.我的构建过程采用模式并创建 bean.我想通过将模式中的一个 complexTypes 映射到 java.util.Map 来自定义此过程.我在某处读到 JAXB不能做接口".我不确定这是否适用于这种情况,但如果是这样,我可以将它映射到 HashMap.此外,它不一定是映射到 Map 的 complexType,如果需要,它可以是一个元素.

I have an existing XML schema that contains a type that I would like to map to a Java Map of some sort using JAXB. My build process takes the schema and creates the beans. I would like to customize this process by having one of the complexTypes in my schema map to a java.util.Map. I've read somewhere that JAXB "can't do interfaces." I'm not sure if that applies in this case, but if so I'd be fine with it mapping to a HashMap. Also, it doesn't necessarily have to be the complexType that maps to a Map, it could be an element if that's what's required.

我使用 .xjb 文件控制我的 JAXB 生成,所以我正在寻找一些 <jaxb:bindings> 放在那里.我尝试了以下方法,但不起作用:

I control my JAXB generation using a .xjb file, so I'm looking for some <jaxb:bindings> to put in there. I've tried the following, but it does not work:

<jaxb:bindings schemaLocation="myschema.xsd" node="/xs:schema">
    <jaxb:globalBindings>
        <jaxb:serializable uid="1"/>
    </jaxb:globalBindings>
    <jaxb:schemaBindings>
        <jaxb:package name="com.myschema.client.types"/>
    </jaxb:schemaBindings>
    <jaxb:bindings node="//xs:complexType[@name='MapType']">
            <jaxb:javaType name="java.util.HashMap"
            parseMethod="com.myschema.common.MapConverter.parseObjectToMap"
            printMethod="com.myschema.common.MapConverter.printMapToObject" />
    </jaxb:bindings>
</jaxb:bindings>

我已经添加了有关我已经尝试过的上述绑定的更多详细信息.它在架构编译期间生成以下错误:

I've added more detail on the above binding that I've already tried. It generates the following error during schema compile:

[jaxb] [ERROR] compiler was unable to honor this javaType customization. It is attached to a wrong place, or its inconsistent with other bindings.

<小时>

将不起作用,因为它只能用于 XML Schema 原语和 Java 类型之间的映射.因为我想在复杂类型和Java类型之间进行映射,所以我不能使用这个.


<jaxb:javaType> will not work because it can only be used for mapping between XML Schema primitives and Java types. Since I want to map between a complex type and a Java type, I cannot use this.

推荐答案

您的 XJC 文件正在使用标准"javaType 指令,我认为该指令仅限于在字符串值与代表之间进行转换java类型.因此,它只适用于转换简单的元素和属性内容.

Your XJC file is using the "standard" javaType directive, which I believe is limited to converting String values to and from a representative java type. As such, it's only suitable for converting simple element and attribute content.

XJC 工具提供了 javaType 的增强版本,理论上应该能够处理更复杂的结构.不幸的是,这尚未实施:

The XJC tool provides an enhanced version of javaType, which in theory should be able to handle more complex structures. Unfortunately, this has yet to be implemented:

https://java.net/jira/browse/JAXB-209(未解决)

例如,能够映射一个HashMap 的架构类型可能是对使用的人的可能要求模型驱动的设计/实现.AFAICS,这在今天是不可能的.因此,必须手动编辑生成的代码.

For instance, being able to map a schema type to a HashMap might be a likely requirement for those using model driven design/implementation. AFAICS, this is not possible today. Therefore, one must manually edit the generated code.

看起来你运气不好.

这篇关于是否可以使用 JAXB 从模式映射到 java.util.Map?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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