我正在尝试从 XSD 模式生成一些 Java 类。我确切地知道我想用 Java 生成什么,并且我正在尝试编写相应的 XSD 架构。
我需要表示一个 java.util.HashMap (HashMap)。 我找不到如何在 XSD 架构(或 xjb 绑定(bind)文件)中指定我想要 Java 中的 HasMap。它总是生成一个列表..
这里是我要生成的代码
@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>
但还是继续生成一个java.util.List的mapEntry:
在我的“错误”类中: protected map 详情 = new Map();
代替
protected java.util.Map<String, String> details = new HashMap<String, String>();
生成的“ map ”类是:
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "map", propOrder = {"mapEntry"})
public class Map {
protected List<MapEntry> mapEntry;
我真的需要为我的应用程序使用 map 。 知道我该怎么做吗?
注意:我也尝试过使用 Oracle owi:hasmp但出现命名空间错误。
xmlns:owi="http://www.oracle.com/webservices/internal" (also tried with xmlns:owi="http://www.oracle.com/webservices/internal/literal")
包含在我的架构声明中
我的“details”元素声明如下
<xsd:element name="details" type="owi:hashmap" />
错误是:
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”关联到任何类型定义组件。
有什么想法吗?
最佳答案
是的,jaxb 可以无缝处理映射,但只能以一种方式处理。
解决方法如下:
http://todayguesswhat.blogspot.co.uk/2012/09/jaxb-xsd-to-java-maphashmap-example.html
但是如果您已经有一个可以正确映射的类,那就麻烦多了。为什么要从 XSD 重新生成它?
关于java - XSD 转 Java,指定使用 Java HashMap,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28718112/