Class JAXBContext

java.lang.Object
jakarta.xml.bind.JAXBContext

public abstract class JAXBContext extends Object
The JAXBContext class provides the client's entry point to the Jakarta XML Binding API. It provides an abstraction for managing the XML/Java binding information necessary to implement the Jakarta XML Binding binding framework operations: unmarshal, marshal and validate.

A client application normally obtains new instances of this class using one of these two styles for newInstance methods, although there are other specialized forms of the method available:

  • JAXBContext.newInstance( "com.acme.foo:com.acme.bar" )
    The JAXBContext instance is initialized from a list of colon separated Java package names. Each java package contains Jakarta XML Binding mapped classes, schema-derived classes and/or user annotated classes. Additionally, the java package may contain Jakarta XML Binding package annotations that must be processed. (see JLS, Section 7.4.1 "Named Packages").
  • JAXBContext.newInstance( com.acme.foo.Foo.class )
    The JAXBContext instance is initialized with class(es) passed as parameter(s) and classes that are statically reachable from these class(es). See newInstance(Class...) for details.

The provider must call the DatatypeConverter.setDatatypeConverter api prior to any client invocations of the marshal and unmarshal methods. This is necessary to configure the datatype converter that will be used during these operations.

Unmarshalling

The Unmarshaller class provides the client application the ability to convert XML data into a tree of Java content objects. The unmarshal method allows for any global XML element declared in the schema to be unmarshalled as the root of an instance document. Additionally, the unmarshal method allows for an unrecognized root element that has an xsi:type attribute's value that references a type definition declared in the schema to be unmarshalled as the root of an instance document. The JAXBContext object allows the merging of global elements and type definitions across a set of schemas (listed in the contextPath). Since each schema in the schema set can belong to distinct namespaces, the unification of schemas to an unmarshalling context must be namespace independent. This means that a client application is able to unmarshal XML documents that are instances of any of the schemas listed in the contextPath. For example:

      JAXBContext jc = JAXBContext.newInstance( "com.acme.foo:com.acme.bar" );
      Unmarshaller u = jc.createUnmarshaller();
      FooObject fooObj = (FooObject)u.unmarshal( new File( "foo.xml" ) ); // ok
      BarObject barObj = (BarObject)u.unmarshal( new File( "bar.xml" ) ); // ok
      BazObject bazObj = (BazObject)u.unmarshal( new File( "baz.xml" ) ); // error, "com.acme.baz" not in contextPath
 

The client application may also generate Java content trees explicitly rather than unmarshalling existing XML data. For all Jakarta XML Binding-annotated value classes, an application can create content using constructors. For schema-derived interface/implementation classes and for the creation of elements that are not bound to a Jakarta XML Binding-annotated class, an application needs to have access and knowledge about each of the schema derived ObjectFactory classes that exist in each of java packages contained in the contextPath. For each schema derived java class, there is a static factory method that produces objects of that type. For example, assume that after compiling a schema, you have a package com.acme.foo that contains a schema derived interface named PurchaseOrder. In order to create objects of that type, the client application would use the factory method like this:

       com.acme.foo.PurchaseOrder po =
           com.acme.foo.ObjectFactory.createPurchaseOrder();
 

Once the client application has an instance of the the schema derived object, it can use the mutator methods to set content on it.

For more information on the generated ObjectFactory classes, see Section 4.2 Java Package of the specification.

The provider must generate a class in each package that contains all of the necessary object factory methods for that package named ObjectFactory as well as the static newInstance( javaContentInterface ) method

Marshalling

The Marshaller class provides the client application the ability to convert a Java content tree back into XML data. There is no difference between marshalling a content tree that is created manually using the factory methods and marshalling a content tree that is the result an unmarshal operation. Clients can marshal a java content tree back to XML data to a java.io.OutputStream or a java.io.Writer. The marshalling process can alternatively produce SAX2 event streams to a registered ContentHandler or produce a DOM Node object. Client applications have control over the output encoding as well as whether or not to marshal the XML data as a complete document or as a fragment.

Here is a simple example that unmarshals an XML document and then marshals it back out:

        JAXBContext jc = JAXBContext.newInstance( "com.acme.foo" );

        // unmarshal from foo.xml
        Unmarshaller u = jc.createUnmarshaller();
        FooObject fooObj = (FooObject)u.unmarshal( new File( "foo.xml" ) );

        // marshal to System.out
        Marshaller m = jc.createMarshaller();
        m.marshal( fooObj, System.out );
 

Validation

In Jakarta XML Binding, the Unmarshaller has included convenience methods that expose the JAXP javax.xml.validation framework. Please refer to the Unmarshaller.setSchema(javax.xml.validation.Schema) API for more information.

Jakarta XML Binding Runtime Framework Compatibility

The following JAXB 1.0 restriction only applies to binding schema to interfaces/implementation classes. Since this binding does not require a common runtime system, a Jakarta XML Binding client application must not attempt to mix runtime objects (JAXBContext, Marshaller, etc. ) from different providers. This does not mean that the client application isn't portable, it simply means that a client has to use a runtime system provided by the same provider that was used to compile the schema.

Discovery of Jakarta XML Binding implementation

To create an instance of JAXBContext, one of JAXBContext.newInstance(...) methods is invoked. After JAX-B implementation is discovered, call is delegated to appropriate provider's method createContext(...) passing parameters from the original call.

JAX-B implementation discovery happens each time JAXBContext.newInstance is invoked. If there is no user specific configuration provided, default JAX-B provider must be returned.

Implementation discovery consists of following steps:

  1. If the system property JAXB_CONTEXT_FACTORY exists, then its value is assumed to be the provider factory class. This phase of the look up enables per-JVM override of the Jakarta XML Binding implementation.
  2. If the property JAXB_CONTEXT_FACTORY exists in the Map<String, ?> passed to newInstance(Class[], Map) or to newInstance(String, ClassLoader, Map), then its value is assumed to be the fully qualified provider factory class name. This phase of the look up enables context sensitive selection of the Jakarta XML Binding implementation.
  3. Provider of JAXBContextFactory is loaded using the service-provider loading facilities, defined by the ServiceLoader class, to attempt to locate and load an implementation of the service using the default loading mechanism: the service-provider loading facility will use the current thread's context class loader to attempt to load the context factory. If the context class loader is null, the system class loader will be used.
    In case of service configuration error a JAXBException will be thrown.
  4. Finally, if all the steps above fail, then the rest of the look up is unspecified. That said, the recommended behavior is to simply look for some hard-coded platform default Jakarta XML Binding implementation. This phase of the look up is so that the environment can have its own Jakarta XML Binding implementation as the last resort.

Once the provider factory class is discovered, context creation is delegated to one of its createContext(...) methods.

Since:
1.6, JAXB 1.0
See Also: