Make your own free website on

  Java Architecture for XML Binding (JAXB)

Following is a description of JAXB technology, as described in JDK 1.6.0 documentation.

The Java Architecture for XML Binding (JAXB) provides an API and tools that automate the mapping between XML documents and Java objects.

The JAXB framework enables developers to perform the following operations:

1) Unmarshal XML content into a Java representation.
2) Access and update the Java representation.
3) Marshal the Java representation of the XML content into XML content.

JAXB gives Java developers an efficient and standard way of mapping between XML and Java code. Java developers using JAXB are more productive because they can write less code themselves and do not have to be experts in XML. JAXB makes it easier for developers to extend their applications with XML and Web Services technologies.

In this article, I'll showcase an example (a JAXB marshalling example) which I ran with the JAXB implementation provided with JDK 1.6.0.

JDK 1.6.0 provides a binding compiler (XJC), which generates Java type definitions corresponding to a XSD schema.

Step 1 - Run the binding compiler on a XSD schema

Start with the following Schema (which represents a Product catalogue) - catalogue.xsd.

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema  xmlns:xs="" elementFormDefault="qualified" attributeFormDefault="unqualified">

    <xs:element name="Products">
                <xs:element name="Product" maxOccurs="unbounded">
                            <xs:element name="ProductId" type="xs:integer" />
                            <xs:element name="ProductCategory" type="xs:string" />
                            <xs:element name="ProductName" type="xs:string" />
                            <xs:element name="Price" type="xs:double" />


xjc catalogue.xsd

The above command generates following files within the folder generated.

Step 2 - Compile the .java files created in Step 1

Compile the above .java files with Java compiler "javac", which will create .class files.

Step 3 - Write a program to Marshal Java objects into XML content

Following is a sample program ( which Marshalls Java objects into XML content:

import generated.*;

//JDK imports
import java.util.*;
import java.math.BigInteger;

//JAXB imports
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;

public class CatalogueMain {

    public static void main(String[] args) {

        ObjectFactory of = new ObjectFactory();

        Products products = of.createProducts();
        Products.Product po1 = of.createProductsProduct();
        po1.setProductId(new BigInteger("100"));
        po1.setProductCategory("Computer Monitor");
        po1.setProductName("LG 700E Color Monitor");

        Products.Product po2 = of.createProductsProduct();
        po2.setProductId(new BigInteger("101"));
        po2.setProductCategory("Computer Hard Disk");
        po2.setProductName("40 GB Seagate Hard Disk");


        //create a Marshaller, and marshal to standard output
            JAXBContext jc = JAXBContext.newInstance("generated");
            Marshaller m = jc.createMarshaller();
            m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
            m.marshal(products, System.out);
        catch(Exception ex)


When the above program is run as:
java CatalogueMain

The output produced is:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
        <ProductCategory>Computer Monitor</ProductCategory>
        <ProductName>LG 700E Color Monitor</ProductName>
        <ProductCategory>Computer Hard Disk</ProductCategory>
        <ProductName>40 GB Seagate Hard Disk</ProductName>

Related links:

1) - Provides reference implementation of the JAXB APIs.

2) - A nice introduction to JAXB.

3) - An open source implementation of JAXB, from Apache.
PS: The last release of this project is shown to be in Oct, 2006. Therefore I do not advice it for production deployments, as it has not kept current with the latest JAXB specification.
The JAXB reference implementation, 1) should be good enough for production.

4) Other Java-XML data binding frameworks:
    *    XMLBeans

    *    Castor


Last Updated: Mar 09, 2009