JAXB Tutorial

JAXB tutorial for beginners and professinals, jaxb example, java architecture for xml binding tutorial, java architecture for xml binding example, jaxb marshalling object into xml example

JAXB Tutorial

JAXB Tutorial

JAXB tutorial provides concepts and API to convert object into XML and XML into object. Our JAXB tutorial is designed for beginners and professionals.

JAXB stands for Java Architecture for XML Binding. It provides mechanism to marshal (write) java objects into XML and unmarshal (read) XML into object. Simply, you can say it is used to convert java object into xml and vice-versa.


Features of JAXB 2.0

JAXB 2.0 includes several features that were not present in JAXB 1.x. They are as follows:

1) Annotation support: JAXB 2.0 provides support to annotation so less coding is required to develop JAXB application. The javax.xml.bind.annotation package provides classes and interfaces for JAXB 2.0.

2) Support for all W3C XML Schema features: it supports all the W3C schema unlike JAXB 1.0.

3) Additional Validation Capabilities: it provides additional validation support by JAXP 1.3 validation API.

4) Small Runtime Library: it required small runtime library that JAXB 1.0.

5) Reduction of generated schema-derived classes: it reduces a lot of generated schema-derived classes.


Simple JAXB Marshalling Example: Converting Object into XML

Let's see the steps to convert java object into XML document.

  • Create POJO or bind the schema and generate the classes
  • Create the JAXBContext object
  • Create the Marshaller objects
  • Create the content tree by using set methods
  • Call the marshal method
File: Employee.java
  1. import javax.xml.bind.annotation.XmlAttribute;  
  2. import javax.xml.bind.annotation.XmlElement;  
  3. import javax.xml.bind.annotation.XmlRootElement;  
  4.   
  5. @XmlRootElement  
  6. public class Employee {  
  7.     private int id;  
  8.     private String name;  
  9.     private float salary;  
  10.   
  11. public Employee() {}  
  12. public Employee(int id, String name, float salary) {  
  13.     super();  
  14.     this.id = id;  
  15.     this.name = name;  
  16.     this.salary = salary;  
  17. }  
  18. @XmlAttribute  
  19. public int getId() {  
  20.     return id;  
  21. }  
  22. public void setId(int id) {  
  23.     this.id = id;  
  24. }  
  25. @XmlElement  
  26. public String getName() {  
  27.     return name;  
  28. }  
  29. public void setName(String name) {  
  30.     this.name = name;  
  31. }  
  32. @XmlElement  
  33. public float getSalary() {  
  34.     return salary;  
  35. }  
  36. public void setSalary(float salary) {  
  37.     this.salary = salary;  
  38. }  
  39.  

@XmlRootElement specifies the root element for the xml document.

@XmlAttribute specifies the attribute for the root element.

@XmlElement specifies the sub element for the root element.

File: ObjectToXml.java
  1. import java.io.FileOutputStream;  
  2.   
  3. import javax.xml.bind.JAXBContext;  
  4. import javax.xml.bind.Marshaller;  
  5.   
  6.   
  7. public class ObjectToXml {  
  8. public static void main(String[] args) throws Exception{  
  9.     JAXBContext contextObj = JAXBContext.newInstance(Employee.class);  
  10.   
  11.     Marshaller marshallerObj = contextObj.createMarshaller();  
  12.     marshallerObj.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);  
  13.   
  14.     Employee emp1=new Employee(1,"Vimal Jaiswal",50000);  
  15.       
  16.     marshallerObj.marshal(emp1, new FileOutputStream("employee.xml"));  
  17.        
  18. }  
  19. }  

Output:

The generated xml file will look like this:

File: employee.xml
  1. <?xml version="1.0" encoding="UTF-8" standalone="yes"?>  
  2. <employee id="1">  
  3.     <name>Vimal Jaiswal</name>  
  4.     <salary>50000.0</salary>  
  5. </employee>  

Simple JAXB UnMarshalling Example: Converting XML into Object

File: XMLToObject.java
  1. import java.io.File;  
  2. import javax.xml.bind.JAXBContext;  
  3. import javax.xml.bind.JAXBException;  
  4. import javax.xml.bind.Unmarshaller;  
  5.   
  6. public class XMLToObject {  
  7. public static void main(String[] args) {  
  8.      try {    
  9.             File file = new File("employee.xml");    
  10.             JAXBContext jaxbContext = JAXBContext.newInstance(Employee.class);    
  11.          
  12.             Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();    
  13.             Employee e=(Employee) jaxbUnmarshaller.unmarshal(file);    
  14.             System.out.println(e.getId()+" "+e.getName()+" "+e.getSalary());  
  15.               
  16.           } catch (JAXBException e) {e.printStackTrace(); }    
  17.          
  18. }  
  19. }  

Output:

1 Vimal Jaiswal 50000.0