users@jaxb.java.net

Customize java.lang.Object Bindings

From: Shelley <randomshelley_at_gmail.com>
Date: Mon, 30 Jul 2007 17:05:14 -0500

My content tree contains an @XmlElement of type Object, which should be
mapped to the XML type, anySimpleType.

    @XmlElement(required = true, nillable = true)
    protected Object value;

During marshalling, if an unknown Java type object has been set (unknown per
the "Default Data Type Bindings"), a MarshalException will be thrown. (ie:
MarshalException "Caused by: javax.xml.bind.JAXBException: class <
java.util.ArrayList> nor any of its super class is known to this context.")

I would like to prevent this exception from occurring, and allow any Object
type to be set and successfully marshalled/unmarshalled. If the type is not
a default data type, the value should be converted to a String (either
object.toString() or some custom logic to determine the appropriate String
based on the class), and the XML type should be xs:anySimpleType.

For example, for the following content tree:
    List<String> list = new ArrayList<String>(2);
    list.add("a");
    list.add("b");
    myXmlType.setValue(list);
the XML output should be as follows:
    <value xmlns:xsi=" http://www.w3.org/2001/XMLSchema-instance" xmlns:xs="
http://www.w3.org/2001/XMLSchema" xsi:type="xs:anySimpleType">[a, b]</value>

A possible solution would involve creation of a custom XmlJavaTypeAdapter to
customize this binding, however, this would currently require a great deal
of rework of the existing logic which parses through the known data types
(ex: com.sun.xml.bind.v2.runtime.JAXBContextImpl.getBeanInfo()). The
DatatypeConverter contains print and parse methods for anySimpleType, but
these methods only use String parameters and return values (not Objects).

Any suggestions on how to accomplish this customized binding between Object
and anySimpleType would be appreciated. Thank you!