Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to use JAXB to map from schema to a java.util.Map?

I have an existing XML schema that contains a type that I would like to map to a Java Map of some sort using JAXB. My build process takes the schema and creates the beans. I would like to customize this process by having one of the complexTypes in my schema map to a java.util.Map. I've read somewhere that JAXB "can't do interfaces." I'm not sure if that applies in this case, but if so I'd be fine with it mapping to a HashMap. Also, it doesn't necessarily have to be the complexType that maps to a Map, it could be an element if that's what's required.

I control my JAXB generation using a .xjb file, so I'm looking for some <jaxb:bindings> to put in there. I've tried the following, but it does not work:

<jaxb:bindings schemaLocation="myschema.xsd" node="/xs:schema">
    <jaxb:globalBindings>
        <jaxb:serializable uid="1"/>
    </jaxb:globalBindings>
    <jaxb:schemaBindings>
        <jaxb:package name="com.myschema.client.types"/>
    </jaxb:schemaBindings>
    <jaxb:bindings node="//xs:complexType[@name='MapType']">
            <jaxb:javaType name="java.util.HashMap"
            parseMethod="com.myschema.common.MapConverter.parseObjectToMap"
            printMethod="com.myschema.common.MapConverter.printMapToObject" />
    </jaxb:bindings>
</jaxb:bindings>

Edit: I've added more detail on the above binding that I've already tried. It generates the following error during schema compile:

[jaxb] [ERROR] compiler was unable to honor this javaType customization. It is attached to a wrong place, or its inconsistent with other bindings.

<jaxb:javaType> will not work because it can only be used for mapping between XML Schema primitives and Java types. Since I want to map between a complex type and a Java type, I cannot use this.

like image 900
Zach Avatar asked Dec 10 '09 15:12

Zach


People also ask

What is the use of Xsd file in Java?

xsd is the XML schema you will use as input to the JAXB binding compiler, and from which schema-derived JAXB Java classes will be generated. For the Customize Inline and Datatype Converter examples, this file contains inline binding customizations.

Which component or tool in JAXB can generate Java files from a XML schema?

After the Java artifacts for your application are generated, you can generate fully annotated Java classes from an XML schema file by using the JAXB schema compiler, xjc command-line tool.

What Java Architecture for XML Binding JAXB annotation should you use when you want to prevent the mapping of a JavaBean property to its XML representation?

1.7) @XmlTransient Prevents the mapping of a JavaBean property/type to XML representation.

What is map in Java Util?

The map interface is present in java. util package represents a mapping between a key and a value. The Map interface is not a subtype of the Collection interface. Therefore it behaves a bit differently from the rest of the collection types. A map contains unique keys.


2 Answers

Your XJC file is using the "standard" javaType directive, which I believe is limited to converting String values to and from a representative java type. As such, it's only suitable for converting simple element and attribute content.

The XJC tool provides an enhanced version of javaType, which in theory should be able to handle more complex structures. Unfortunately, this has yet to be implemented:

https://java.net/jira/browse/JAXB-209 (unresolved)

For instance, being able to map a schema type to a HashMap might be a likely requirement for those using model driven design/implementation. AFAICS, this is not possible today. Therefore, one must manually edit the generated code.

Looks like you're out of luck.

like image 189
skaffman Avatar answered Oct 03 '22 19:10

skaffman


Have you looked at example here (Customized mapping of HashMap): http://java.sun.com/javase/6/docs/api/javax/xml/bind/annotation/adapters/XmlAdapter.html

Need to create custom XmlAdapter I'm afraid.

like image 45
maximdim Avatar answered Oct 03 '22 19:10

maximdim