Pages

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"?>  
 <bookStore>  
   <books>  
     <book bookId="1">  
       <name>book1</name>  
       <author>author1</author>  
     </book>  
     <book bookId="2">  
       <name>book2</name>  
       <author>author2</author>  
     </book>  
   </books>  
 </bookStore>  

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;  
   @XmlElement  
   @XmlJavaTypeAdapter(BookAdapter.class)  
   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;  
   @XmlAttribute  
   public String getBookId() {  
     return bookId;  
   }  
   public void setBookId(String bookId) {  
     this.bookId = bookId;  
   }  
   @XmlElement  
   public String getName() {  
     return name;  
   }  
   public void setName(String name) {  
     this.name = name;  
   }  
   @XmlElement  
   public String getAuthor() {  
     return author;  
   }  
   public void setAuthor(String author) {  
     this.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  
   @Override  
   public Map<String, Book> unmarshal(Books v) throws Exception {  
     Map<String, Book> map = new HashMap<String, Book>();  
     for(Book book : v.getBooks()){  
       map.put(book.getBookId(),book);  
     }  
     return map;  
   }  
   @Override  
   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());  
         System.out.println("=========================");  
       }  
     } catch (JAXBException e) {  
       e.printStackTrace();  
     }  
   }  
 }  

Full Project : https://github.com/rajithd/jaxb-sample


1 comments:

sarabjeet said...

i really like that you are giving information on core and advance java concepts. Being enrolled at http://www.wiziq.com/course/1779-core-and-advance-java-concepts i found your information very helpful indeed.thanks for it.

Post a Comment