Advance JAXB - XML into Java Map

In this article I will show you how to decode XML into a java Hashmap. So always there are multiple ways. But I will show you how you can do this in a simple and more responsible way.

JAXB is referred as Java Architecture for XML Binding. In this tutorial we are only use annotation for that. We don't require any third party libraries for this because java provide all the additional JAXB annotation for us.

Here is some good article about JAXB

OK first thing first.
Lets create a XML file.
 <?xml version="1.0" encoding="UTF-8" standalone="yes"?>  
     <book bookId="1">  
     <book bookId="2">  

Here is the sample and most popular tutorial XML :P

So after this we will write three domain classes for this and a adapter class to get the map.
Yes we need to have 3 domain java classes -> BookStore, Books and Book
and BookAdapter java class to create the hashmap.

Here is the BookStore class.
 @XmlRootElement(name = "bookStore")  
 public class BookStore {  
   private HashMap<String,Book> books;  
   public HashMap<String, Book> getBooks() {  
     return books;  
   public void setBooks(HashMap<String, Book> books) {  
     this.books = books;  

Some basic JAXB annotation are there with @XmlJavaTypeAdapter. We use this adapter class to marshal and unmarshal the xml.
So here is the Books java class
 public class Books {  
   private ArrayList<Book> books;  
   @XmlElement(name = "book")  
   public ArrayList<Book> getBooks() {  
     return books;  
   public void setBooks(ArrayList<Book> books) {  
     this.books = books;  

And here is the Book java class
 public class Book {  
   private String bookId;  
   private String name;  
   private String author;  
   public String getBookId() {  
     return bookId;  
   public void setBookId(String bookId) {  
     this.bookId = bookId;  
   public String getName() {  
     return name;  
   public void setName(String name) { = name;  
   public String getAuthor() {  
     return author;  
   public void setAuthor(String author) { = author;  

So the basic are done. Now we have to write the adapter class to do the magic(actually there is no magic at all.)

Here is the BookAdapter java class
 public class BookAdapter extends XmlAdapter<Books,Map<String,Book>> {  
   // we only going to change this method. Because we are only doing un marshaling  
   // xml into map  
   public Map<String, Book> unmarshal(Books v) throws Exception {  
     Map<String, Book> map = new HashMap<String, Book>();  
     for(Book book : v.getBooks()){  
     return map;  
   public Books marshal(Map<String, Book> v) throws Exception {  
     return null;  

So in here we only consider about unmarshal because our objective is to decode xml into hashmap.

And finally here is the App class with main method
 public class App {  
   public static void main(String[] args) {  
     File file = new File("book-store.xml");  
     try {  
       JAXBContext jaxbContext = JAXBContext.newInstance(BookStore.class);  
       Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();  
       BookStore bookStore = (BookStore) unmarshaller.unmarshal(file);  
       HashMap<String, Book> bookMap = bookStore.getBooks();  
       for (Map.Entry<String, Book> entry : bookMap.entrySet()) {  
         System.out.println("Key = " + entry.getKey());  
         System.out.println("value = " + entry.getValue().getAuthor() + " " + entry.getValue().getName());  
     } catch (JAXBException e) {  

Full Project :

No comments:

Post a Comment