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="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">

    <xs:element name="Products">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="Product" maxOccurs="unbounded">
                    <xs:complexType>
                        <xs:sequence>
                            <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" />
                        </xs:sequence>
                    </xs:complexType>
                </xs:element>
            </xs:sequence>
        </xs:complexType>
    </xs:element>

</xs:schema>

Run XJC
xjc catalogue.xsd

The above command generates following files within the folder generated.
ObjectFactory.java
Products.java

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 (CatalogueMain.java) which Marshalls Java objects into XML content:

import generated.*;

//JDK imports
import java.util.*;
import java.io.*;
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");
        po1.setPrice(5600);

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

        products.getProduct().add(po1);
        products.getProduct().add(po2);

        //create a Marshaller, and marshal to standard output
        try
        {
            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)
        {
            ex.printStackTrace();
        }
    }

}


When the above program is run as:
java CatalogueMain

The output produced is:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Products>
    <Product>
        <ProductId>100</ProductId>
        <ProductCategory>Computer Monitor</ProductCategory>
        <ProductName>LG 700E Color Monitor</ProductName>
        <Price>5600.0</Price>
    </Product>
    <Product>
        <ProductId>101</ProductId>
        <ProductCategory>Computer Hard Disk</ProductCategory>
        <ProductName>40 GB Seagate Hard Disk</ProductName>
        <Price>3000.0</Price>
    </Product>
</Products>


Related links:
1) https://jaxb.dev.java.net/ - Provides reference implementation of the JAXB APIs
2) http://java.sun.com/developer/technicalArticles/WebServices/jaxb/ - A nice introduction to JAXB
3) http://ws.apache.org/jaxme/index.html - An open source implementation of JAXB, from Apache.
(The last release of this project is shown to be as Oct, 2006. Therefore I won't 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.)


Home


Last Updated: Dec 27, 2009