Using JAXB to Marshal a Plain Java Object Into XML And Printing to Console and File

This tutorial has three files:

  • Java Object: MyJAXBObject.java
  • Main Class: JavaToXMLUsingJAXB.java
  • JAXB Index File: jaxb.index (content of this file is a class name ‘MyJAXBObject’ (without quotes)

All of these files are in the same directory.

  • JDK Version: 1.5

Jar Dependencies:

jsr173_api.jar
jaxb-apijar

The fully compiled and tested classes are give below:

package com.kushal.xml;
import java.io.FileOutputStream;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
/**
 * @author KushalP
 * www.sanjaal.com/java
 * This class demonstrates the use of JAXB (JAVA API for XML Binding) to:
 * -- Convert a java object into XML file
 * -- Ouput the XML file to console and physical file
 *
 * The following jar files were used:
 * jsr173_api.jar
 * jaxb-apijar
 */
public class JavaToXMLUsingJAXB {

	public static void main(String[] args) throws Exception {
	    /**
	     * Create a context with a package name of where objects are located
	     */
		JAXBContext context = JAXBContext.newInstance("com.kushal.xml");

		/**
		 * Create an XML Marshaller
		 */
	    Marshaller m = context.createMarshaller();
	    /**
	     * Set the Marshaller property
	     * Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE formats output XML
	     */
	    m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);

	    /**
	     * Create an instance of the object which has to be converted to XML file.
	     * You should create a jaxb.index file in the package defined above
	     * and list the Objects (class names without .java or .class) in that file.
	     */
	    MyJAXBObject object = new MyJAXBObject();
	    /**
	     * Set the object properties
	     */
	    object.setMyField1("Tim Baker");
	    object.setMyField2("New Jersey");
	    object.setMyIntegerField(678);

	    /**
	     * Marshal the object to the console.
	     */
	    m.marshal(object, System.out);

	    /**
	     * Marshalling the same object to the physical file
	     */
	    m.marshal(object, new FileOutputStream("C:/temp/myJAXBgenerated.xml"));
	  }
	}

Here is the plain Object with three fields:

package com.kushal.xml;

/**
 * This is a simple object that contains three fields.
 *
 * It is intended to be instantiated and converted to XML file
 * using JAXB (JAVA API for XML Binding)
 *
 * Uses Java Annotation.
 * Requires the project compatible to jdk 1.5 or above
 * Otherwise you will get annotation errror.
 */
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class MyJAXBObject {
	private String myField1;

	private String myField2;

	private int myIntegerField;

	public String getMyField1() {
		return myField1;
	}

	public void setMyField1(String myField1) {
		this.myField1 = myField1;
	}

	public String getMyField2() {
		return myField2;
	}

	public void setMyField2(String myField2) {
		this.myField2 = myField2;
	}

	public int getMyIntegerField() {
		return myIntegerField;
	}

	public void setMyIntegerField(int value) {
		this.myIntegerField = value;
	}

}


Also, you need to have a jaxb.index file created in the same package as the objects with the following content:

MyJAXBObject

When you run this program, the XML file will be generated and printed to both console and file. The following is the output of the generated XML file.