This documentation differs from the official API. Jadeite adds extra features to the API including: variable font sizes, constructions examples, placeholders for classes and methods, and auto-generated “See Also” links. Additionally it is missing some items found in standard Javadoc documentation, including: generics type information, “Deprecated” tags and comments, “See Also” links, along with other minor differences. Please send any questions or feedback to bam@cs.cmu.edu.


javax.xml.bind.annotation.adapters
class XmlAdapter

java.lang.Object extended by javax.xml.bind.annotation.adapters.XmlAdapter
Direct Known Subclasses:
CollapsedStringAdapter, HexBinaryAdapter, NormalizedStringAdapter

public abstract class XmlAdapter
extends Object

Adapts a Java type for custom marshaling.

Usage:

Some Java types do not map naturally to a XML representation, for example HashMap or other non JavaBean classes. Conversely, a XML repsentation may map to a Java type but an application may choose to accesss 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 JAXB binding framework during marshaling and unmarshalling:

Writing an adapter therefore involves the following steps:

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.

     <hashmap>
         <entry key="id123">this is a value</entry>
         <entry key="id312">this is another value</entry>
         ...
       </hashmap>  
 

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

     
     <xs:complexType name="myHashMapType">
       <xs:sequence>
         <xs:element name="entry" type="myHashMapEntryType"
                        minOccurs = "0" maxOccurs="unbounded"/>
       </xs:sequence>
     </xs:complexType>

     <xs:complexType name="myHashMapEntryType">
       <xs:simpleContent>
         <xs:extension base="xs:string">
           <xs:attribute name="key" type="xs:int"/>
         </xs:extension>
       </xs:simpleContent>
     </xs:complexType>

 

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

     public class MyHashMapType {
         List<MyHashMapEntryType> entry;
     }

     public class MyHashMapEntryType {
         @XmlAttribute
         public Integer key; 

         @XmlValue
         public String value;
     }
 

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

     public final class MyHashMapAdapter extends
                        XmlAdapter<MyHashMapType,HashMap> { ... }
      
 

Step 5: Use the adapter.

     public class Foo {
         @XmlJavaTypeAdapter(MyHashMapAdapter.class)
         HashMap hashmap;
         ...
     }
 
The above code fragment will map to the following schema:
     <xs:complexType name="Foo">
       <xs:sequence>
         <xs:element name="hashmap" type="myHashMapType"
       </xs:sequence>
     </xs:complexType>
 


Constructor Summary
protected

          Do-nothing constructor for the derived classes.
 
Method Summary
abstract Object

          Convert a bound type to a value type.
abstract Object

          Convert a value type to a bound type.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

XmlAdapter

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

Method Detail

marshal

public abstract Object marshal(Object v)
                        throws Exception
Convert a bound type to a value type.

Parameters:
v - The value to be convereted. 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 {@link javax.xml.bind.ValidationEventHandler}.

unmarshal

public abstract Object unmarshal(Object 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 {@link javax.xml.bind.ValidationEventHandler}.


This documentation differs from the official API. Jadeite adds extra features to the API including: variable font sizes, constructions examples, placeholders for classes and methods, and auto-generated “See Also” links. Additionally it is missing some items found in standard Javadoc documentation, including: generics type information, “Deprecated” tags and comments, “See Also” links, along with other minor differences. Please send any questions or feedback to bam@cs.cmu.edu.
This page displays the Jadeite version of the documention, which is derived from the offical documentation that contains this copyright notice:
Copyright 2008 Sun Microsystems, Inc. All rights reserved. Use is subject to license terms. Also see the documentation redistribution policy.
The official Sun™ documentation can be found here at http://java.sun.com/javase/6/docs/api/.