Project Description
- In this JAXB example we will see how to marshall and unmarshall list of objects.
Environment Used:
- JDK 6.
- Eclipse Indigo IDE for Java EE Developers.
New project in Eclipse
Create a new Java project in Eclipse IDE and name it as “JAXBList“.
Create Bean class
Create a new Class “Book.java” in the package “com.theopentutorials.jaxb.to” and copy the following code.
package com.theopentutorials.jaxb.to;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "book")
public class Book {
private String name;
private String author;
private String publisher;
private String isbn;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.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;
}
@Override
public String toString() {
return "Book [name=" + name + ", author=" + author + ", publisher="
+ publisher + ", isbn=" + isbn + "]";
}
}
This is a simple bean class containing JAXB annotations whose objects are marshalled and unmarshalled. When a top level class is annotated with @XmlRootElement annotation, then its value is represented as XML element, in our case <book> tag.
@XmlAccessorType annotation allows us to configure the use of fields or properties to access the data in our domain object (Book object). This is specified as an XmlAccessType (PUBLIC_MEMBER, PROPERTY, FIELD, or NONE) via the @XmlAccessorType annotation. We use access type FIELD to cause JAXB implementations to create bindings for fields and annotated properties. So in our case all fields – name, author, publisher, isbn are marshalled/unmarshalled by JAXB.
Create Bean class to hold list of objects
Now we need to a new Class “Books.java” in the package “com.theopentutorials.jaxb.to” to hold the list of Book objects by having an ArrayList instance variable in our class.
package com.theopentutorials.jaxb.to;
import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "books")
public class Books {
@XmlElement(name = "book", type = Book.class)
private List<Book> books = new ArrayList<Book>();
public Books() {}
public Books(List<Book> books) {
this.books = books;
}
public List<Book> getBooks() {
return books;
}
public void setBooks(List<Book> books) {
this.books = books;
}
}
This class is a simple holder of list of book objects. We will use this class to marshal and unmarshal list of objects.
Create JAXB Handler (Helper class)
Create a new Class “JAXBXMLHandler.java” in the package “com.theopentutorials.jaxb.xml” and copy the following code.
package com.theopentutorials.jaxb.xml;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import com.theopentutorials.jaxb.to.Book;
import com.theopentutorials.jaxb.to.Books;
public class JAXBXMLHandler {
// Export
public static void marshal(List<Book> books, File selectedFile)
throws IOException, JAXBException {
JAXBContext context;
BufferedWriter writer = null;
writer = new BufferedWriter(new FileWriter(selectedFile));
context = JAXBContext.newInstance(Books.class);
Marshaller m = context.createMarshaller();
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
m.marshal(new Books(books), writer);
writer.close();
}
// Import
public static List<Book> unmarshal(File importFile) throws JAXBException {
Books books = new Books();
JAXBContext context = JAXBContext.newInstance(Books.class);
Unmarshaller um = context.createUnmarshaller();
books = (Books) um.unmarshal(importFile);
return books.getBooks();
}
}
This is a helper class which has methods to perform marshalling and unmarshalling. These methods are called from client code (in this case, main() method).
Create Java Application client (main())
Create a new Class “JAXBDemo.java” in the package “com.theopentutorials.jaxb.main” and copy the following code.
package com.theopentutorials.jaxb.main;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.JAXBException;
import com.theopentutorials.jaxb.to.Book;
import com.theopentutorials.jaxb.xml.JAXBXMLHandler;
public class JAXBDemo {
public static void main(String[] args) {
Book book = new Book();
book.setAuthor("Kathy Sierra");
book.setName("SCJP");
book.setPublisher("Tata McGraw Hill");
book.setIsbn("856-545456736");
Book book1 = new Book();
book1.setAuthor("Christian Bauer");
book1.setName("Java Persistence with Hibernate");
book1.setPublisher("Manning");
book1.setIsbn("978-3832180577");
List<Book> books = new ArrayList<Book>();
books.add(book);
books.add(book1);
//Marshalling: Writing Java objects to XMl file
try {
JAXBXMLHandler.marshal(books, new File("books.xml"));
} catch (IOException e) {
e.printStackTrace();
} catch (JAXBException e) {
e.printStackTrace();
}
//Unmarshalling: Converting XML content to Java objects
try {
books = JAXBXMLHandler.unmarshal(new File("books.xml"));
} catch (JAXBException e) {
e.printStackTrace();
}
System.out.println(books);
}
}
- This class creates two book objects and stores it in list and calls marshal method from JAXBXMLHandler helper class passing list of objects and the file to write the object.
- We call unmarshal method passing the marshalled file name which returns list of book objects.
- Finally, printing the list of books (books object, which calls toString() from Book class).
Output
Run the JAXBDemo.java.
[Book [name=SCJP, author=Kathy Sierra, publisher=Tata McGraw Hill, isbn=856-545456736],
Book [name=Java Persistence with Hibernate, author=Christian Bauer, publisher=Manning, isbn=978-3832180577]]
Refresh your project in Project Explorer (press F5 on your project) to see the generated XML file.
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<books>
<book>
<name>SCJP</name>
<author>Kathy Sierra</author>
<publisher>Tata McGraw Hill</publisher>
<isbn>856-545456736</isbn>
</book>
<book>
<name>Java Persistence with Hibernate</name>
<author>Christian Bauer</author>
<publisher>Manning</publisher>
<isbn>978-3832180577</isbn>
</book>
</books>
Folder Structure
The complete folder structure of this example is shown below.

