This tutorial is introduction to Java Architecture for XML Binding (JAXB). JAXB tutorial is based on the Java 6.0 and later versions. JAXB is XML Binding API which defines the API for reading and writing the XML files from Java objects. As the binding word defines, XML and Java objects are binded together for the easy conversion of data from XML to Java and Java to XML. JAXB API is developed as a separate project outside JDK. Since Java 6.0, it is part of the Java libraries. Java objects are annotated with the suitable annotations to help the JAXB to convert to XML elements. @XmlRootElement, @XmlType and @XmlElement are the few most frequently used annotations.
@XmlRootElement
This is class level annotation. This also can be used with enum type. When this annotation is used for a class or enum, then that class will appear as element in the XML structure. This annotation has two properties name and namespace. Name can be specified if you want to customize the name instead of using the class name. If namespace is specified, that element will be considered as the root element for that XML structure.
@XmlType
This annotation has five properties, among that propOrder is important one for specifying the sequence of the elements to appear in the XML structure. This annotation also will be used with class or enum types.
@XmlElement
@XmlElement annotation can be used with a Javabean property or non-static, non-transient field. When this annotation is used, the bean property is used as the local attribute for a complex type. It has six properties as name, defaultValue,namespace,nillable, required and type. We will create a sample project for demonstrating the above concepts.
As a first step, create a domain model. We are creating Employee.java and Company.java
Example JAXB Tutorial Listing 1 : Employee.java
package javabeat.net; import javax.xml.bind.annotation.XmlRootElement; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlType; @XmlRootElement(name="employee") @XmlType (propOrder={"name","empId","designation","city","country"}) public class Employee { private String name; private String empId; private String designation; private String city; private String country; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getEmpId() { return empId; } public void setEmpId(String empId) { this.empId = empId; } @XmlElement (name="role") public String getDesignation() { return designation; } public void setDesignation(String designation) { this.designation = designation; } public String getCity() { return city; } public void setCity(String city) { this.city = city; } public String getCountry() { return country; } public void setCountry(String country) { this.country = country; } }
Example JAXB Tutorial Listing 2 : Company.java
package javabeat.net; import java.util.ArrayList; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlRootElement; @XmlRootElement (namespace="org.xml.ns") public class Company { private ArrayList<Employee> employees; private String branchCode; private String city; public ArrayList<Employee> getEmployees() { return employees; } public void setEmployees(ArrayList<Employee> employees) { this.employees = employees; } public String getBranchCode() { return branchCode; } public void setBranchCode(String branchCode) { this.branchCode = branchCode; } public String getCity() { return city; } public void setCity(String city) { this.city = city; } }
Create the test client program for executing the JAXB example.
Example JAXB Tutorial Listing 1 : ClientMain.java
package javabeat.net; 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 ClientMain { private static final String XML_FILE_NAME = "CompanyEmployees.xml"; public static void main (String args[]) throws JAXBException, IOException{ ArrayList<Employee> arrayList = new ArrayList<Employee>(); Employee employee = new Employee(); employee.setName("Krishna"); employee.setEmpId("001"); employee.setDesignation("Software Engineer"); employee.setCity("Bangalore"); employee.setCountry("India"); arrayList.add(employee); employee = new Employee(); employee.setName("Sanaulla"); employee.setEmpId("002"); employee.setDesignation("Software Engineer"); employee.setCity("Chennai"); employee.setCountry("India"); arrayList.add(employee); Company company = new Company(); company.setEmployees(arrayList); JAXBContext jaxbContext = JAXBContext.newInstance(Company.class); Marshaller marshaller = jaxbContext.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); marshaller.marshal(company, System.out); marshaller.marshal(company, new File (XML_FILE_NAME)); Unmarshaller unmarshaller = jaxbContext.createUnmarshaller(); Company companyObj = (Company) unmarshaller.unmarshal(new FileReader(XML_FILE_NAME)); ArrayList<Employee> list = companyObj.getEmployees(); for (Employee employeeObj : list) { System.out.println("Employee: " + employeeObj.getName() + " from " + employeeObj.getName()); } } }
If you run the above code, Java objects are created in the XML file using the marshaling, then it is un-marshaled back to the Java objects. It is clear enough to understand yourself if you go through the above example programs. If you have questions on this tutorial, please reach me by submitting your comments. I would be happy to answer all your queries.