Welcome to JAXB Example Tutorial. Java Architecture for XML Binding (JAXB) provides API for converting Object to XML and XML to Object easily. JAXB was developed as a separate project but it was used widely and finally became part of JDK in Java 6.
JAXB Tutorial
This tutorial is based on Java 7 and current JAXB version 2.0.
JAXB Marshalling: Converting a Java Object to XML.
JAXB Unmarhsalling: Converting XML to Java Object.
Using JAXB is very easy and it uses java annotations. We need to annotate Java Object to provide instructions for XML creation and then we have to create Marshaller
to convert Object to XML.
Unmarshaller
is used to convert XML to java Object.
JAXBContext
is the entry point for JAXB and provides methods to get marshaller and unmarshaller object.
Some basic and useful JAXB annotations are:
- @XmlRootElement: This is a must have annotation for the Object to be used in JAXB. It defines the root element for the XML content.
- @XmlType: It maps the class to the XML schema type. We can use it for ordering the elements in the XML.
- @XmlTransient: This will make sure that the Object property is not written to the XML.
- @XmlAttribute: This will create the Object property as attribute.
- @XmlElement(name = “abc”): This will create the element with name “abc”
There are some other JAXB annotation that you can learn from JAXB Official Site.
JAXB Example
Let’s run a simple JAXB example program to demonstrate the use of JAXB marshalling and unmarshalling.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
package com.journaldev.xml.jaxb; import javax.xml.bind.annotation.XmlAttribute; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlRootElement; import javax.xml.bind.annotation.XmlTransient; import javax.xml.bind.annotation.XmlType; @XmlRootElement(name = "Emp") @XmlType(propOrder = {"name", "age", "role", "gender"}) public class Employee { private int id; private String gender; private int age; private String name; private String role; private String password; @XmlTransient public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } @XmlAttribute public int getId() { return id; } public void setId(int id) { this.id = id; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } @XmlElement(name = "gen") public String getGender() { return gender; } public void setGender(String gender) { this.gender = gender; } public String getRole() { return role; } public void setRole(String role) { this.role = role; } @Override public String toString() { return "ID = " + id + " NAME=" + name + " AGE=" + age + " GENDER=" + gender + " ROLE=" + role + " PASSWORD=" + password; } } |
Employee is a normal java bean with private fields and their getters and setters. Notice the use of JAXB annotations to define root element, element name, elements order and transient property.
Here is a test JAXB example program showing JAXB Marshalling and JAXB Unmarshalling.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
package com.journaldev.xml.jaxb; import java.io.File; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; import javax.xml.bind.Marshaller; import javax.xml.bind.Unmarshaller; public class JAXBExample { private static final String FILE_NAME = "jaxb-emp.xml"; public static void main(String[] args) { Employee emp = new Employee(); emp.setId(1); emp.setAge(25); emp.setName("Pankaj"); emp.setGender("Male"); emp.setRole("Developer"); emp.setPassword("sensitive"); jaxbObjectToXML(emp); Employee empFromFile = jaxbXMLToObject(); System.out.println(empFromFile.toString()); } private static Employee jaxbXMLToObject() { try { JAXBContext context = JAXBContext.newInstance(Employee.class); Unmarshaller un = context.createUnmarshaller(); Employee emp = (Employee) un.unmarshal(new File(FILE_NAME)); return emp; } catch (JAXBException e) { e.printStackTrace(); } return null; } private static void jaxbObjectToXML(Employee emp) { try { JAXBContext context = JAXBContext.newInstance(Employee.class); Marshaller m = context.createMarshaller(); //for pretty-print XML in JAXB m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); // Write to System.out for debugging // m.marshal(emp, System.out); // Write to File m.marshal(emp, new File(FILE_NAME)); } catch (JAXBException e) { e.printStackTrace(); } } } |
Above program creates following jaxb-emp.xml file.
1 2 3 4 5 6 7 8 9 |
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <Emp id="1"> <name>Pankaj</name> <age>25</age> <role>Developer</role> <gen>Male</gen> </Emp> |
Check that XML file don’t have password field and we get following output when same XML file is unmarshalled to Object.
1 2 3 |
ID = 1 NAME=Pankaj AGE=25 GENDER=Male ROLE=Developer PASSWORD=null |
That’s all for JAXB example tutorial, as you can see that it’s very easy to use.