XSD to Java, specify to use a Java HashMap

hiveship picture hiveship · Feb 25, 2015 · Viewed 15.4k times · Source

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.

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

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:

In my "Error" class: 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: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")

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

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

Any idea ?

Answer

Zielu picture Zielu · Feb 25, 2015

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

The solution is described here:

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?