Untitled Document

Marshalling POJOs with JAXB

After reading this How-To document, you will be able to sse TopLink 11 to

  • Marshall plain Java objects (POJOs) to XML.

Software Requirements

Conventions

The following conventions are used in this document:

  • <DEMO_HOME> - the directory where this How-To document will be used.
  • <ANT_HOME> - the directory where Apache ANT is installed..

Introduction

JAXB defines annotations to control the mapping of Java objects to XML, but it also defines a default set of mappings.  Using the defaults, TopLink can marshall a set of objects into XML and unmarshall an XML documenet into objects.  This is particularly useful when you have an existing domain model and you need an XML representation of those objects.  This how-to describes how to use TopLink to marshall an object and it's contained objects to XML using the default JAXB mappings.  It also shows how to unmarshall an XML document into an exisiting Java domain model.

Step 1: Setup

Unzip JAXBPojos.zip into the <DEMO_HOME> folder of your choice.  After unzipping, <DEMO_HOME> will contain the folder JAXBPojos.  <DEMO_HOME>/JAXBPojos/build.xml is an ANT build file that can be used to build and run this how-to.  Open build.xml in a text editor and set the property "toplink.home" to reference your TopLink install.  It should look something like:
    <property name="toplink.home" value="C:\bin\Toplink11"/>

Step 2: Creating a JAXBContext

Open  <DEMO_HOME>/JAXBPojos/src/examples/jaxb/marshall/MarshallPojos.java in a text editor and review the main() method.  The top of the method looks like:
public static void main(String[] args) throws Exception {
Class[] classes = new Class[3];
classes[0] = Address.class;
classes[1] = Customer.class;
classes[2] = PhoneNumber.class
JAXBContext jaxbContext = JAXBContext.newInstance(classes);
Marshaller marshaller = jaxbContext.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,Boolean.TRUE);
The JAXBContext is created with an array of classes that will be included in the context.  All classes statically referenced  by the listed cases will also be included in the context and can be marshalled and unmarshalled. In the example, with the context initialized as in the method, the Address, Customer, and PhoneNumber classes will be included in the context.  Once created, a marshaller can e created for converting objects to XML.  

If you open the Address, Customer, and PhoneNumber classes you'll notice that there are no JAXB mappings or imports.  The classes are simple POJOs and so the mappings for the classes are all defaulted.

Step 3: Selecting the TopLink 11 JAXB runtime

When you create a JAXBContext you need to identify the JAXB runtime to use.  To use TopLink 11 you need to provide a jaxb.properties file in the directory that contains the domain model classes.  In the example, those classes are in the examples.jaxb.pojo package and so the <DEMO_HOME>/JAXBPojos/src/examples/jaxb/pojo directory should contain the jaxb.properties and contain:
javax.xml.bind.context.factory = oracle.toplink.ox.jaxb20.JAXBContextFactory

Step 4: Marshalling Java to  XML

Once created with the classes explictly listed, the context can be used to marshall objects.  In the example, a set of objects are created and associated.  In the expert below, a customer object is created and marshalled to XML on the console.
 Customer customer = new Customer();
customer.setFirstName("William");
customer.setLastName("Gibson");
System.out.println("\nCUSTOMER");
marshaller.marshal(customer, System.out);

The resulting XML looks like:

<?xml version="1.0" encoding="UTF-8"?>
<customer>
<firstName>William</firstName>
<lastName>Gibson</lastName>
</customer>

You can use the ANT build.xml file to compile the example and marshall objects. To do so
you need to invoke ANT with the "marshall" target.  From a command prompt, with your current
directory <DEMO_HOME>/JAXBPojos (the directory that contains build.xml), you can issue the command:
<ANT_HOME>/bin/ant marshall

Summary

In this how-to we've seen how to write a Java program to marshall unannotated Java POJOs to XML.  The important steps are to:
  • Create the JAXBContext with an array of classses you intend to marshall.
  • Define the JAXB runtime in the jaxb.properties file in the package containing your domain classes.
E-mail this page
Printer View Printer View
Oracle Is The Information Company About Oracle | Oracle RSS Feeds | Careers | Contact Us | Site Maps | Legal Notices | Terms of Use | Privacy