問題描述
我正在嘗試從 XSD 模式生成一些 Java 類.我確切地知道我想用 Java 生成什么,我正在嘗試編寫相應(yīng)的 XSD 架構(gòu).
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.
我需要表示一個(gè) java.util.HashMap (HashMap).我找不到如何在 XSD 模式(或 xjb 綁定文件)中指定我想要 Java 中的 HasMap.它總是生成一個(gè)列表..
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>();
我試過這個(gè):
<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>
但它還是會繼續(xù)生成一個(gè)mapEntry的java.util.List:
But it still continue to generate a java.util.List of mapEntry:
在我的錯(cuò)誤"課程中:受保護(hù)的地圖詳情 = 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;
我真的需要為我的應(yīng)用程序使用地圖.知道我該怎么做嗎?
I really need to use a map for my application. Any idea about how I can do ?
注意:我也嘗試過使用 Oracle owi:hasmp 但出現(xiàn)命名空間錯(cuò)誤.
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")
包含在我的架構(gòu)聲明中
我的詳細(xì)信息"元素聲明如下
and my "details" element declared as below
<xsd:element name="details" type="owi:hashmap" />
錯(cuò)誤是:
src-resolve.4.2:解析組件owi:hasmap"時(shí)出錯(cuò).它是檢測到owi:hasmap"在命名空間中
'http://www.oracle.com/webservices/internal',但是來自這個(gè)的組件命名空間不可從架構(gòu)文檔中引用'文件://myFile.xsd.如果這是不正確的命名空間,也許'owi:hasmap' 的前綴需要更改.如果這是正確的命名空間,然后應(yīng)添加適當(dāng)?shù)膶?dǎo)入"標(biāo)簽'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"關(guān)聯(lián)到任何類型定義組件.
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
但是如果你已經(jīng)有一個(gè)可以正確映射的類,那就很麻煩了.為什么要從 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?
這篇關(guān)于XSD 轉(zhuǎn) Java,指定使用 Java HashMap的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!