是否可以使用 JAXB 从模式映射到 java.util.Map?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1881712/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is it possible to use JAXB to map from schema to a java.util.Map?
提问by Zach
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.
我有一个现有的 XML 模式,其中包含我想使用 JAXB 映射到某种 Java 映射的类型。我的构建过程采用模式并创建 bean。我想通过将模式中的一个 complexTypes 映射到 java.util.Map 来自定义此过程。我在某处读到 JAXB“不能做接口”。我不确定这是否适用于这种情况,但如果是这样,我可以将它映射到 HashMap。此外,它不一定是映射到 Map 的 complexType,如果需要,它可以是一个元素。
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:
我使用 .xjb 文件控制我的 JAXB 生成,所以我正在寻找一些<jaxb:bindings>放在那里。我尝试了以下方法,但不起作用:
<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>
Edit: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>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.
<jaxb:javaType>将不起作用,因为它只能用于XML Schema 原语和 Java 类型之间的映射。因为我想在复杂类型和 Java 类型之间进行映射,所以我不能使用它。
采纳答案by skaffman
Your XJC file is using the "standard" javaTypedirective, 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指令,我认为该指令仅限于将 String 值与具有代表性的 java 类型相互转换。因此,它仅适用于转换简单的元素和属性内容。
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:
XJC 工具提供了 的增强版本javaType,理论上应该能够处理更复杂的结构。不幸的是,这尚未实施:
https://java.net/jira/browse/JAXB-209(unresolved)
https://java.net/jira/browse/JAXB-209(未解决)
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.
例如,能够将模式类型映射到 HashMap 可能是那些使用模型驱动设计/实现的人的要求。AFAICS,这在今天是不可能的。因此,必须手动编辑生成的代码。
Looks like you're out of luck.
看来你倒霉了。
回答by maximdim
Have you looked at example here (Customized mapping of HashMap): http://java.sun.com/javase/6/docs/api/javax/xml/bind/annotation/adapters/XmlAdapter.html
您是否看过此处的示例(HashMap 的自定义映射):http: //java.sun.com/javase/6/docs/api/javax/xml/bind/annotation/adapters/XmlAdapter.html
Need to create custom XmlAdapter I'm afraid.
恐怕需要创建自定义 XmlAdapter。

