Skip Headers

# Using the TopLink XML Context

Previous

Next

TopLink provides an `XMLContext` class with which you can create instances of TopLink `XMLMarshaller`, `XMLUnmarshaller`, and `XMLValidator`.

The `XMLContext` is thread-safe. For example, if multiple threads accessing the same `XMLContext` object request an `XMLMarshaller`, each will receive their own instance of `XMLMarshaller`, so any state that the `XMLMarshaller` maintains will be unique to that process. The same is true of instances of `XMLUnmarshaller` and `XMLValidator`.

By using the `XMLContext`, you can use TopLink XML in multithreaded architectures, such as the binding layer for Web services.

To use the TopLink `XMLContext`, do the following:

1. Configure your application classpath to include your domain object class files (see "Understanding JAXB-Specific Generated Files") and the TopLink runtime.

2. Acquire the session manager (see "Acquiring the Session Manager").

3. Use the session manager to acquire your XML session using the `sessions.xml` file you generated with the TopLink JAXB compiler (see "Acquiring a Session From the Session Manager").

4. Get the XML project instance from the session:

       Project myProject = session.getProject();

5. Create a TopLink XML context instance with the project:

       XMLContext context = new XMLContext(myProject);

6. Use the `XMLContext` to create a TopLink `XMLMarshaller`, `XMLUnmarshaller`, and `XMLValidator`:

       XMLMarshaller marshaller = context.createMarshaller();
       marshaller.marshal(myObject, outputStream);
       marshaller.setFormattedOutput(true);

       XMLUnmarshaller unmarshaller = context.createUnmarshaller();
       Employee emp = (Employee)unmarshaller.unmarshal(new File("employee.xml"));

       XMLValidator validator = context.createValidator();
       boolean isValid = validator.validate(emp);