Creating XML Using JAXB


In this post we will show a simple example on how we can create XML using JAXB.

JAXB “constitutes a convenient framework for processing XML documents, providing significant benefits as compared to previously available methods such as the one following the Document Object Model (DOM). In the DOM approach, the parser creates a tree of objects that represents the content and organization of data in the document. The application can then navigate through the tree in memory to access the data it needs. DOM data, however, is contained in objects of a single type, linked according to the XML document’s structure, with individual node objects containing an element, an attribute, a CDATA section, etc. Values are invariably provided as strings.” For more details see the JAXB documentation.

In the following example, we used the Reference Implementation (RI) that can be downloaded from the JAXB site.  The download contains just a Jar file, thus we need to execute this jar to use the JAXB. To perform it, you might go through the command line and execute the following:

java -jar JAXB2_XXXXXX.jar

Once you have done it, it will create a directory which contains the utility tools and required jars to use the JAXB framework. After that, we need to create an Environment Variable called JAXB_HOME that must point to the JAXB installation directory. In my case it is: C:\jaxb-ri-20060426.

Let’s start with our JAXB Hello World.

To start, we need to create a XML Schema that will define how our XML document structure will be like. You can see it below:


Basically the XML Schema defines that we will have a element course that has  a subelement called students that may have until 5 students.

To generate the JAXB artifacts we will use the xjc compiler, you can find it into bin directory inside your JAXB installation directory. By a command line you can execute this:

xjc -p course Course.xsd

This will create a directory called course that will contain the necessary classes to read or create an XML based on the xsd passed to the xjc compiler, in our case the Course.xsd that contains the XML Schema shown.

Now we need just to use the generated classes to create our XML. Below you can see a simple example showing how we could use the generated artifacts.


To run the example we need to put in the class path the JAXB jars, you can find them in the lib directory in your JAXB installation directory.

Once you execute the example you’ll get an XML in your console, if we format the XML we get this:


So, as we could see we can create XML documents in a simple way using JAXB.



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s