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.
Book.java
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) {
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;
}
}
2. BookStore.java
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 "Bookstore.java" 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) {
this.name = name;
}
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
}
3. BookMain
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
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.setIsbn("978-0060554736");
book1.setName("The Game");
book1.setAuthor("Neil Strauss");
book1.setPublisher("Harpercollins");
bookList.add(book1);
Book book2 = new Book();
book2.setIsbn("978-3832180577");
book2.setName("Feuchtgebiete");
book2.setAuthor("Charlotte Roche");
book2.setPublisher("Dumont Buchverlag");
bookList.add(book2);
// create BookStore, assigning book
BookStore BookStore = new BookStore();
BookStore.setName("Fraport BookStore");
BookStore.setLocation("Frankfurt Airport");
BookStore.setBookList(bookList);
// 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();
System.out.println("Output from our XML File: ");
Unmarshaller um = context.createUnmarshaller();
BookStore bookstore2 = (BookStore) um.unmarshal(new FileReader(
BOOKSTORE_XML));
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">
<bookList>
<book>
<author>Neil Strauss</author>
<titleName>The Game</titleName>
<publisher>Harpercollins</publisher>
<isbn>978-0060554736</isbn>
</book>
<book>
<author>Charlotte Roche</author>
<titleName>Feuchtgebiete</titleName>
<publisher>Dumont Buchverlag</publisher>
<isbn>978-3832180577</isbn>
</book>
</bookList>
<location>Frankfurt Airport</location>
<name>Fraport BookStore</name>
</ns2:bookStore>
Output from our XML File:
Book: The Game from Neil Strauss
Book: Feuchtgebiete from Charlotte Roche