Easy learning with example program codes

JAXB marshalling – convert java object to xml example using one pojo

Marshalling is the process of converting java object into xml file. Let us discuss JAXB marshalling process with below example. The marshal() method of JAXB Marshaller is used for marshalling process.


1. Create a pojo class.
2. create JAXB context instance.
3. Create Marshaller instance using JAXB context.
4. Call marshal() method for marshalling process.

Example explanation:

In below example we have created a pojo class. Student class is annotated with @XmlRootElement annotation, so Student will be the root element of the xml. Setter methods of name, className and rollNo properties are annotated with @XmlElement annotation, so these properties will be the elements of xml. Setter of age property is annotated with @XmlTransient annotation, so it will not be included in xml. Setter of id is annotated with @XmlAttribute, So it will be the attribute of the root element in xml file.


import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;
 * This class represents a Student.
 * @author codesjava
public class Student {	
	private String name;
	private String rollNo;
	private String className;
	private int age;
	private int id;
	//Default constructor
	public Student(){
	//Parameterised constructor
	public Student(String name, String rollNo, 
			String className, int age, int id){ = name;
		this.rollNo = rollNo;
		this.className = className;
		this.age = age; = id;
	public String getName() {
		return name;
	public void setName(String name) { = name;
	public String getClassName() {
		return className;
	public void setClassName(String className) {
		this.className = className;
	public String getRollNo() {
		return rollNo;
	public void setRollNo(String rollNo) {
		this.rollNo = rollNo;
	public int getAge() {
		return age;
	public void setAge(int age) {
		this.age = age;
	public int getId() {
		return id;
	public void setId(int id) { = id;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
 * This is test class for Marshalling.
 * @author codesjava
public class JAXBTest {
  public static void main(String args[]){   
    try {
	//Create Student object
	Student student = 
		new Student("jai", "MCA/07/06", "MCA final", 27, 6);
	//create JAXB context
	JAXBContext context = JAXBContext.newInstance(Student.class);
	//Create Marshaller using JAXB context
	Marshaller marshaller = context.createMarshaller();
	marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
	//Do the marshal operation
                            new FileOutputStream("D:\\Student.xml"));
	System.out.println("java object converted to xml successfully.");
     } catch (Exception e) {


java object converted to xml successfully.

Download this example.
Next Topic: JAXB marshalling – convert java object to xml example using multiple pojo.
Previous Topic: JAXB annotation overview.


Please follow and like us:
Posted in JAXB   

Copyright © 2020 CodesJava Protection Status