Wednesday, November 28, 2012

My JAXB Learnings - Part 2

As part of this post, you will be understanding on the following concepts in the JAXB through an example.

1. Defining the name space
2. Defining the wrapper elements in the XML
3. Setting the name for entities in the list.
4. Defining the order for elements

This example is based on the book store and book model objects.

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

@XmlRootElement(name = "myBook")
// 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 = "titleName")
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;



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.kondal.bookstore.model")
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;

3. BookMain 

import java.util.ArrayList;

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

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("The Game");
book1.setAuthor("Neil Strauss");

Book book2 = new Book();
book2.setAuthor("Charlotte Roche");
book2.setPublisher("Dumont Buchverlag");

// create BookStore, assigning book
BookStore BookStore = new BookStore();
BookStore.setName("Fraport BookStore");
BookStore.setLocation("Frankfurt 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);

// Write to System.out
m.marshal(BookStore, System.out);

// Write to File
m.marshal(BookStore, new File(BOOKSTORE_XML));

// 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(
ArrayList<Book> list = bookstore2.getBooksList();
for (Book book : list) {
System.out.println("Book: " + book.getName() + " from "
+ book.getAuthor());

Output on console:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns2:bookStore xmlns:ns2="com.kondal.bookstore.model">
            <author>Neil Strauss</author>
            <titleName>The Game</titleName>
            <author>Charlotte Roche</author>
            <publisher>Dumont Buchverlag</publisher>
    <location>Frankfurt Airport</location>
    <name>Fraport BookStore</name>

Output from our XML File:
Book: The Game from Neil Strauss
Book: Feuchtgebiete from Charlotte Roche

No comments:

Post a Comment