javax.xml.bind

Class JAXBContext

public abstract class JAXBContext extends Object

The JAXBContext provides the JAXB users anchor to the implmentation and hos generated classes. A JAXBContext is used to obtain instances of Marshaller, Unmarshaller, and Validator. To obtain a JAXBContext, the application invokes

   JAXBContext context = JAXBContext.newInstance("com.mycompany:com.mycompany.xml");
 
The list of colon separated package names matches the list in the schemas used to generate classes. In other words: If you have a schema using package name "com.mycompany.xml", then this package name has to be part of the list.

The JAXBContext class will scan the given list of packages for a file called jaxb.properties. This file contains the name of an instantiation class in the property JAXB_CONTEXT_FACTORY. (See JAXBContext for details on how the class name is obtained.) Once such a file is found, the given class is loaded via ClassLoader#loadClass(java.lang.String). The JAXBContext class demands, that the created object has a method

   public static JAXBContext createContext(String pPath, ClassLoader pClassLoader)
     throws JAXBException;
 
This method is invoked with the same path and ClassLoader than above. See newInstance} for details on the choice of the ClassLoader.

The created context will scan the same package path for implementation specific configuration details (in the case of the JaxMe application a file called Configuration.xml in any of the packages) and do whatever else is required to initialize the runtime. In particular it will invoke setDatatypeConverter.

Since: JAXB1.0

Author: JSR-31

See Also: Marshaller Unmarshaller Validator

Field Summary
static StringJAXB_CONTEXT_FACTORY

This is the name of the property used to determine the name of the initialization class: "javax.xml.bind.context.factory".

Method Summary
abstract MarshallercreateMarshaller()

Creates a new instance of Marshaller.

abstract UnmarshallercreateUnmarshaller()

Creates a new instance of Unmarshaller.

abstract ValidatorcreateValidator()

Creates a new instance of Validator.

static JAXBContextnewInstance(String pPath)

Creates a new instance of JAXBContext by applying the following algorithm:

  1. The first step is to determine the name of an initialization class.
static JAXBContextnewInstance(String pPath, ClassLoader pClassLoader)

Creates a new instance of JAXBContext by applying the following algorithm:

  1. The first step is to determine the name of an initialization class.

Field Detail

JAXB_CONTEXT_FACTORY

public static final String JAXB_CONTEXT_FACTORY

This is the name of the property used to determine the name of the initialization class: "javax.xml.bind.context.factory". The name is used by JAXBContext and newInstance. It contains a class name. The class must contain a static method

   public static JAXBContext createContext(String, ClassLoader) throws JAXBException;
 
which is invoked to create the actual instance of JAXBContext.

Method Detail

createMarshaller

public abstract Marshaller createMarshaller()

Creates a new instance of Marshaller. The Marshaller can be used to convert JAXB objects into XML data.

Note: Marshallers are reusable, but not reentrant (thread safe).

createUnmarshaller

public abstract Unmarshaller createUnmarshaller()

Creates a new instance of Unmarshaller. The Unmarshaller can be used to convert XML data into JAXB objects.

Note: Unmarshallers are reusable, but not reentrant (thread safe).

createValidator

public abstract Validator createValidator()

Creates a new instance of Validator. The Validator can be used to validate JAXB objects.

newInstance

public static JAXBContext newInstance(String pPath)

Creates a new instance of JAXBContext by applying the following algorithm:

  1. The first step is to determine the name of an initialization class. For any of the package names in the list given by pPath the JAXBContext class will try to find a file called jaxb.properties. This file's got to be in standard property file format. The JAXBContext class will load the property file.
  2. A property called "javax.xml.bind.context.factory" (see JAXB_CONTEXT_FACTORY) is evaluated. It must contain the name of an initialization class. The initialization class is loaded via Thread.currentThread().getContextClassLoader().loadClass(String).
  3. The initialization class must contain a method
           public static JAXBContext createContext(String, ClassLoader) throws JAXBException;
         
    which is invoked with the pPath argument and the ClassLoader of the JAXBContext class as parameters. The result of this method is also used as the result of the newInstance(String) method.

Parameters: pPath A colon separated path of package names where to look for jaxb.properties files. The package names must match the generated classes which you are going to use in your application.

Returns: An initialized instance of JAXBContext.

Throws: JAXBException An error occurred while creating the JAXBContext instance.

newInstance

public static JAXBContext newInstance(String pPath, ClassLoader pClassLoader)

Creates a new instance of JAXBContext by applying the following algorithm:

  1. The first step is to determine the name of an initialization class. For any of the package names in the list given by pPath the JAXBContext class will try to find a file called jaxb.properties. This file's got to be in standard property file format. The JAXBContext class will load the property file.
  2. A property called "javax.xml.bind.context.factory" (see JAXB_CONTEXT_FACTORY) is evaluated. It must contain the name of an initialization class. The initialization class is loaded via pClassLoader.loadClass(String).
  3. The initialization class must contain a method
           public static JAXBContext createContext(String, ClassLoader) throws JAXBException;
         
    which is invoked with the parameters pPath and pClassLoader. The result of this method is also used as the result of the newInstance(String) method.

Parameters: pPath A colon separated path of package names where to look for jaxb.properties files. The package names must match the generated classes which you are going to use in your application.

Returns: An initialized instance of JAXBContext.

Throws: JAXBException An error occurred while creating the JAXBContext instance.