Friday, February 27, 2015

JAXB & Xml Binding Using Annotations

Create an entity bean and annotate it for JAXB Xml binding:
package com.noushin.soaws.model;

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

@XmlType(propOrder={"name", "author", "isbn", "publisher"})
public class Book {

    private String name;
    private String author;
    private String isbn;
    private String publisher;
    public Book() {}
    public Book(String author) { = author;        
    public String getAuthor() {
        return author;
    public void setAuthor(String author) { = author;

    public String getName() {
        return name;

    public void setName(String name) { = name;

    public String getIsbn() {
        return isbn;

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

    public String getPublisher() {
        return publisher;

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


Create a class to manage the entity bean, read to and write from XML files:
package com.noushin.soaws.service;

import java.util.ArrayList;
import java.util.List;

import javax.jws.WebService;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;

import com.noushin.soaws.model.Book;

public class BookService {

    ArrayList<Book> books;

    public void setBooks() {
        books = 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");


    public ArrayList<Book> getBooks() {
        return books;

    public Book getBook() {
        return new Book("Mark Twain");

    public static void manage() {
        BookService bookService = new BookService();
        // create JAXB context and instantiate marshaller
        JAXBContext context;
        try {
            context = JAXBContext.newInstance(BookService.class);

            Marshaller m = context.createMarshaller();
            m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);

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

            // Write to File
            m.marshal(bookService, new File("/tmp/books.xml"));

            // get variables from our xml file, created before
            System.out.println("Output from our XML File: ");
            Unmarshaller um = context.createUnmarshaller();
            BookService bookService2 = (BookService) um.unmarshal(new FileReader("books.xml"));
            ArrayList<Book> list = bookService2.getBooks();
            for (Book book : list) {
                System.out.println("Book: " + book.getName() + " from " + book.getAuthor());
        catch (JAXBException e) {
            // TODO Auto-generated catch block
        catch (FileNotFoundException e) {
            // TODO Auto-generated catch block

Using Junit, write a test case to test the functionality:
package com.noushin.soaws.service;

import static org.junit.Assert.*;

import org.junit.Test;

public class BookServiceTest {

    public void test() {
        BookService bookService = new BookService();


No comments:

Post a Comment