Home » JAXB » JAXB Overview

JAXB Overview

What Is JAXB :

JAXB stands for Java architecture for XML binding.It is used to convert XML to java object and java object to XML.
JAXB defines an API for reading and writing Java objects to and from XML documents.
It provides mechanism to marshal (write) java objects into XML and unmarshal (read) XML into object.

There are two operations you can perform using JAXB

  1. Marshalling :Converting a java object to XML
  2. UnMarshalling :Converting a XML to java object


JAXB Marshalling Example: Converting Object into XML :

Create a new Java project called "JAXBMARSHALLING"
Create POJO Class Employee inside com.example.jaxb package.
No extra jaxb libraries are required if you are using JDK1.6 or above, because JAXB is bundled in JDK 1.6.

For JDK < 1.6, download JAXB from here, and add "jaxb-api.jar" and "jaxb-impl.jar" on your project classpath.

Employee.java


package com.example.jaxb;

import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class Employee {
	private int id;
	private String name;
	private float salary;
	private String email;
	private String department;

	public Employee() {

	}

	public Employee(int id, String name, float salary, String email,
			String department) {
		this.id = id;
		this.name = name;
		this.salary = salary;
		this.email = email;
		this.department = department;
	}

	@XmlAttribute
	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	@XmlElement
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@XmlElement
	public float getSalary() {
		return salary;
	}

	public void setSalary(float salary) {
		this.salary = salary;
	}

	@XmlElement
	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	@XmlElement
	public String getDepartment() {
		return department;
	}

	public void setDepartment(String department) {
		this.department = department;
	}
}

In the above code

  • @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.


Convert Object to XML(Marshling)

MarshlingTest.java


package com.example.jaxb;

import java.io.File;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;

public class MarshlingTest {
	public static void main(String[] args) {
		Employee employee = new Employee();
		employee.setId(101);
		employee.setName("Mukesh");
		employee.setDepartment("Finance");
		employee.setSalary(50000);
		employee.setEmail("m@gmail.com");
		try {
			 
			File file = new File("E:\\employee.xml");
			JAXBContext jaxbContext = JAXBContext.newInstance(Employee.class);
			Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
	 
			// output pretty printed
			jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
	 
			jaxbMarshaller.marshal(employee, file);
			jaxbMarshaller.marshal(employee, System.out);
	 
		      } catch (JAXBException e) {
			e.printStackTrace();
		      }
	 
		}
	}

Output :

After running the program employee.xml file will generate in "E" drive


<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<employee id="101">
    <department>Finance</department>
    <email>m@gmail.com</email>
    <name>Mukesh</name>
    <salary>50000.0</salary>
</employee>

Convert XML to Object :

Here we are going to convert employee.xml content into Employee Object.The jaxbMarshaller.unmarshal() contains overloaded methods to achieve this.

MarshlingTest.java


package com.example.jaxb;

import java.io.File;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;

public class UnMarshlingExample {
	public static void main(String[] args) {

		try {

			File file = new File("E:\\employee.xml");
			JAXBContext jaxbContext = JAXBContext.newInstance(Employee.class);

			Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
			Employee employee = (Employee) jaxbUnmarshaller.unmarshal(file);
			System.out.println("Employee Name: " + employee.getName());
			System.out.println("Employee Department: "
					+ employee.getDepartment());
			System.out.println("Employee email: " + employee.getEmail());
			System.out.println("Employee Salary: " + employee.getSalary());

		} catch (JAXBException e) {
			e.printStackTrace();
		}
	}
}

Output :

Employee Name: Mukesh
Employee Department: Finance
Employee email: m@gmail.com
Employee Salary: 50000.0

Download the example


Previous Next Article

comments powered by Disqus