XSD到Java,指定使用Java HashMap [英] XSD to Java, specify to use a Java HashMap

查看:92
本文介绍了XSD到Java,指定使用Java HashMap的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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..

这里我要生成的代码

    @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>();

我试过这个:

    <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>

但它仍然会继续生成mapEntry的java.util.List:

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

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

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

而不是

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

生成的地图类是:

@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 ?

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

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

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

包含在我的架构声明中

和我的details元素声明如下

and my "details" element declared as below

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

错误是:


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

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

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

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

任何想法?

推荐答案

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

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

此处描述了解决方案:

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

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

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到Java,指定使用Java HashMap的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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