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

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

问题描述

我有一个现有的XML模式,它包含一个我希望使用JAXB映射到某种Java Map的类型。我的构建过程采用模式并创建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.

我使用a控制我的JAXB生成.xjb文件,所以我正在寻找一些< 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.






< jaxb: javaType> 无效,因为它只能用于 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文件正在使用standard javaType 指令,我认为它仅限于将String值转换为代表性的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天全站免登陆