XSD 转 Java,指定使用 Java HashMap

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28718112/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 14:04:03  来源:igfitidea点击:

XSD to Java, specify to use a Java HashMap

javaxmlxsdjaxbwadl

提问by hiveship

I am trying to generate some Java class from XSD schema. I know exactly what I want to generate in Java, and I'm trying to write the corresponding XSD schema.

我正在尝试从 XSD 模式生成一些 Java 类。我确切地知道我想在 Java 中生成什么,并且我正在尝试编写相应的 XSD 模式。

I need to represent a java.util.HashMap (HashMap). I can't find how to specify in the XSD schema (or xjb binding file) that I want an HasMap in Java. It always generate a List..

我需要表示一个 java.util.HashMap (HashMap)。我找不到如何在 XSD 模式(或 xjb 绑定文件)中指定我想要 Java 中的 HasMap。它总是生成一个列表..

here the code I want to generate

这里是我想生成的代码

    @XmlAccessorType(XmlAccessType.FIELD)
    @XmlType(name = "ErrorMessage", propOrder = { "name", "details"})
    public class ErrorMessage {
        @XmlElement(required = true)
        protected String name;
        @XmlElement(required = false)
        protected java.util.Map<String, String> details = new HashMap<String, String>();

I have tried this:

我试过这个:

    <xsd:complexType name="ErrorMessage">
    <xsd:sequence>
        <xsd:element name="name" type="xsd:string" />
        <xsd:element name="details" type="map" />
    </xsd:sequence>
</xsd:complexType>


<xsd:complexType name="map">
    <xsd:sequence>
        <xsd:element name="mapEntry" type="mapEntry" minOccurs="0" maxOccurs="unbounded" />
    </xsd:sequence>
</xsd:complexType>

<xsd:complexType name="mapEntry">
    <xsd:sequence>
        <xsd:element name="key" type="xsd:string" />
        <xsd:element name="value" type="xsd:string" />
    </xsd:sequence>
</xsd:complexType>

But it still continue to generate a java.util.List of mapEntry:

但是它还是继续生成mapEntry的java.util.List:

In my "Error" class: protected Map details = new Map();

在我的“错误”类中:protected Map details = new Map();

Instead of

代替

protected java.util.Map<String, String> details = new HashMap<String, String>();

And the generated "map" class is :

生成的“地图”类是:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "map", propOrder = {"mapEntry"})
public class Map {
     protected List<MapEntry> mapEntry;

I really need to use a map for my application. Any idea about how I can do ?

我真的需要为我的应用程序使用地图。知道我该怎么做吗?

Note: I have also tried to use Oracle owi:hasmpbut got a namespace error.

注意:我也尝试过使用Oracle owi:hasmp,但出现命名空间错误。

xmlns:owi="http://www.oracle.com/webservices/internal" (also tried with xmlns:owi="http://www.oracle.com/webservices/internal/literal")

included in my schema declaration

包含在我的架构声明中

and my "details" element declared as below

和我的“详细信息”元素声明如下

<xsd:element name="details" type="owi:hashmap" />

The error is:

错误是:

src-resolve.4.2: Error resolving component 'owi:hasmap'. It was detected that 'owi:hasmap' is in namespace
'http://www.oracle.com/webservices/internal', but components from this namespace are not referenceable from schema document 'file://myFile.xsd. If this is the incorrect namespace, perhaps the prefix of 'owi:hasmap' needs to be changed. If this is the correct namespace, then an appropriate 'import' tag should be added to 'file://myFile.xsd

src-resolve.4.2:解析组件“owi:hasmap”时出错。检测到“owi:hasmap”位于命名空间
http://www.oracle.com/webservices/internal”中,但无法从架构文档“file://myFile.xsd”中引用来自该命名空间的组件。如果这是不正确的命名空间,则可能需要更改 'owi:hasmap' 的前缀。如果这是正确的命名空间,则应将适当的“import”标记添加到“file://myFile.xsd”

And it can not associate "owi:hasmap" to any type definition component.

并且它不能将“owi:hasmap”关联到任何类型定义组件。

Any idea ?

任何的想法 ?

采纳答案by Zielu

Yes, maps are handled seamlessly by jaxb, but only in one way.

是的,地图由 jaxb 无缝处理,但只有一种方式。

The solution is described here:

此处描述了解决方案:

http://todayguesswhat.blogspot.co.uk/2012/09/jaxb-xsd-to-java-maphashmap-example.html

http://todayguesswhat.blogspot.co.uk/2012/09/jaxb-xsd-to-java-maphashmap-example.html

But it is a lot of hassle if you already have a class that maps correctly. Why do you want to regenerate it from XSD?

但是如果你已经有一个正确映射的类,那就很麻烦了。为什么要从 XSD 重新生成它?