Monday, March 21, 2011

Jaxb - Example to generate xml from java class

JAXB uses annotations to indicate the central elements.
Following are the annotations used in jaxb:
@XmlRootElement(namespace = "namespace")
Define the root element for a XML tree
@XmlType(propOrder = { "field2", "field1",.. })
Allows to define the order in which the fields are written in the XML file
@XmlElement(name = "neuName")
Define the XML element which will be used. Only need to be used if the neuNeu is different then the JavaBeans Name.

Using the Annotations

Step 1 : Create a project in eclipse called SampleJaxb
Now create a Book class:
package com.vaani.xml.jaxb;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;

@XmlRootElement(name = "book")
// If you want you can define the order in which the fields are written
// Optional
@XmlType(propOrder = { "author", "name", "publisher", "isbn" })
public class Book {

private String name;
private String author;
private String publisher;
private String isbn;

// If you like the variable name, e.g. "name", you can easily change this
// name for your XML-Output:
@XmlElement(name = "bookName")
public String getName() {
return name;

public void setName(String name) { = name;

public String getAuthor() {
return author;

public void setAuthor(String author) { = author;

public String getPublisher() {
return publisher;

public void setPublisher(String publisher) {
this.publisher = publisher;

public String getIsbn() {
return isbn;

public void setIsbn(String isbn) {
this.isbn = isbn;


Now we have our book store:

package com.vaani.xml.jaxb;

import java.util.ArrayList;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;

//This statement means that class "" is the root-element of our example
@XmlRootElement(namespace = "com.vaani.xml.jaxb")
public class Bookstore {

// XmLElementWrapper generates a wrapper element around XML representation
@XmlElementWrapper(name = "bookList")
// XmlElement sets the name of the entities
@XmlElement(name = "book")
private ArrayList<Book> bookList;
private String name;
private String location;

public void setBookList(ArrayList<Book> bookList) {
this.bookList = bookList;

public ArrayList<Book> getBooksList() {
return bookList;

public String getName() {
return name;

public void setName(String name) { = name;

public String getLocation() {
return location;

public void setLocation(String location) {
this.location = location;

The runner class to generate this xml file (we will generate as well print it on console) :

package com.vaani.jaxb.runner;

import java.util.ArrayList;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;

import com.vaani.jaxb.Book;
import com.vaani.jaxb.Bookstore;

public class BookMain {

private static final String BOOKSTORE_XML = "./bookstore-jaxb.xml";

public static void main(String[] args) throws JAXBException, IOException {

ArrayList<Book> bookList = new ArrayList<Book>();

// create books
Book book1 = new Book();
book1.setName("You can win");
book1.setAuthor("Shiv Khera");

Book book2 = new Book();
book2.setName("The monk who sold his Ferrari");
book2.setAuthor("Robin Sharma");
book2.setPublisher("Dumont Buchverlag");

// create bookstore, assigning book
Bookstore bookstore = new Bookstore();
bookstore.setName("Wordsworth Bookstore");
bookstore.setLocation("Mumbai Airport");

// create JAXB context and instantiate marshaller
JAXBContext context = JAXBContext.newInstance(Bookstore.class);
Marshaller m = context.createMarshaller();
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
//print xml to console
m.marshal(bookstore, System.out);

Writer w = null;
try {
w = new FileWriter(BOOKSTORE_XML);
//print xml to file
m.marshal(bookstore, w);
} finally {
try {
} catch (Exception e) {

// get variables from our xml file, created before
System.out.println("Output from our XML File: ");
Unmarshaller um = context.createUnmarshaller();
Bookstore bookstore2 = (Bookstore) um.unmarshal(new FileReader(

for (int i = 0; i < bookstore2.getBooksList().toArray().length; i++) {
System.out.println("Book " + (i + 1) + ": "
+ bookstore2.getBooksList().get(i).getName() + " from "
+ bookstore2.getBooksList().get(i).getAuthor());


Now the generated output is :

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns2:bookstore xmlns:ns2="com.vaani.jaxb">
<author>Shiv Khera</author>
<bookName>You can win</bookName>
<author>Robin Sharma</author>
<bookName>The monk who sold his Ferrari</bookName>
<publisher>Dumont Buchverlag</publisher>
<location>Mumbai Airport</location>
<name>Wordsworth Bookstore</name>

Output from our XML File: 
Book 1: You can win from Shiv Khera
Book 2: The monk who sold his Ferrari from Robin Sharma

No comments:

Post a Comment