XSD 转 Java,指定使用 Java HashMap

时间:2023-01-12
本文介绍了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.它总是生成一个列表..

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:

在我的错误"课程中:受保护的地图详情 = 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")

包含在我的架构声明中

我的详细信息"元素声明如下

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',但是来自这个的组件命名空间不可从架构文档中引用'文件://myFile.xsd.如果这是不正确的命名空间,也许'owi:hasmap' 的前缀需要更改.如果这是正确的命名空间,然后应添加适当的导入"标签'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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

上一篇:DOM Parser 错误 childNodes Count 下一篇:如何创建通用 XSD 类型

相关文章

最新文章