5

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 ?

3
  • The problem is not the hashmap type, rather it's the reference to the xsd describing it. Does this help? stackoverflow.com/questions/12105840/… Commented Feb 25, 2015 at 12:03
  • I have also tried to import the Oracle namespace with xsd:import, but still the same result (can not associate owi:hashmap (or just hashmap to any type definition). Commented Feb 25, 2015 at 12:29
  • I am also facing similar issue now in my application. Were you able to generate Map<String,String> instead of List<>? Commented May 28 at 23:01

1 Answer 1

3

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?

Sign up to request clarification or add additional context in comments.

1 Comment

I am also facing similar issue now in my application. Were you able to generate Map<String,String> instead of List<>?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.