Class XmlAdapter<ValueType,BoundType>

java.lang.Object
jakarta.xml.bind.annotation.adapters.XmlAdapter<ValueType,BoundType>
Type Parameters:
BoundType - The type that Jakarta XML Binding doesn't know how to handle. An adapter is written to allow this type to be used as an in-memory representation through the ValueType.
ValueType - The type that Jakarta XML Binding knows how to handle out of the box.
Direct Known Subclasses:
Adapter1, Adapter1, Adapter10, Adapter11, Adapter12, Adapter13, Adapter14, Adapter15, Adapter16, Adapter17, Adapter18, Adapter19, Adapter2, Adapter2, Adapter20, Adapter3, Adapter3, Adapter4, Adapter4, Adapter5, Adapter5, Adapter6, Adapter6, Adapter7, Adapter7, Adapter8, Adapter8, Adapter9, Adapter9, AnyTypeAdapter, BooleanAdapter, CollapsedStringAdapter, HexBinaryAdapter, Link.JaxbAdapter, MediaTypeAdapter, NormalizedStringAdapter, ReadOnlyAdapter, RuntimeUtil.ToStringAdapter, SwaRefAdapter, SwaRefAdapterMarker, UriAdapter, ZeroOneBooleanAdapter

public abstract class XmlAdapter<ValueType,BoundType> extends Object
Adapts a Java type for custom marshaling.

Usage:

Some Java types do not map naturally to an XML representation, for example HashMap or other non JavaBean classes. Conversely, an XML representation may map to a Java type but an application may choose to access the XML representation using another Java type. For example, the schema to Java binding rules bind xs:DateTime by default to XmlGregorianCalendar. But an application may desire to bind xs:DateTime to a custom type, MyXmlGregorianCalendar, for example. In both cases, there is a mismatch between bound type , used by an application to access XML content and the value type, that is mapped to an XML representation.

This abstract class defines methods for adapting a bound type to a value type or vice versa. The methods are invoked by the Jakarta XML Binding binding framework during marshaling and unmarshalling:

  • XmlAdapter.marshal(...): During marshalling, Jakarta XML Binding binding framework invokes XmlAdapter.marshal(..) to adapt a bound type to value type, which is then marshaled to XML representation.
  • XmlAdapter.unmarshal(...): During unmarshalling, Jakarta XML Binding binding framework first unmarshalls XML representation to a value type and then invokes XmlAdapter.unmarshal(..) to adapt the value type to a bound type.
Writing an adapter therefore involves the following steps:
  • Write an adapter that implements this abstract class.
  • Install the adapter using the annotation XmlJavaTypeAdapter

Example: Customized mapping of HashMap

The following example illustrates the use of @XmlAdapter and @XmlJavaTypeAdapter to customize the mapping of a HashMap.

Step 1: Determine the desired XML representation for HashMap.

Step 2: Determine the schema definition that the desired XML representation shown above should follow.

Step 3: Write value types that can generate the above schema definition.

Step 4: Write the adapter that adapts the value type, MyHashMapType to a bound type, HashMap, used by the application.

Step 5: Use the adapter. The above code fragment will map to the following schema:

Since:
1.6, JAXB 2.0
Author:
  • Sekhar Vajjhala, Sun Microsystems Inc.
  • Kohsuke Kawaguchi, Sun Microsystems Inc.
See Also:
  • Constructor Details

    • XmlAdapter

      protected XmlAdapter()
      Do-nothing constructor for the derived classes.
  • Method Details

    • unmarshal

      public abstract BoundType unmarshal(ValueType v) throws Exception
      Convert a value type to a bound type.
      Parameters:
      v - The value to be converted. Can be null.
      Throws:
      Exception - if there's an error during the conversion. The caller is responsible for reporting the error to the user through ValidationEventHandler.
    • marshal

      public abstract ValueType marshal(BoundType v) throws Exception
      Convert a bound type to a value type.
      Parameters:
      v - The value to be converted. Can be null.
      Throws:
      Exception - if there's an error during the conversion. The caller is responsible for reporting the error to the user through ValidationEventHandler.