• December 16, 2014
  • JAXB
  • Comments Off on JAXB marshalling – convert java object to xml example using one pojo

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 Student.java 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 javawithease 
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){
		this.name = name;
		this.rollNo = rollNo;
		this.className = className;
		this.age = age;
		this.id = id;
	public String getName() {
		return name;
	public void setName(String name) {
		this.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) {
		this.id = id;


import java.io.FileOutputStream;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
 * This is test class for Marshalling.
 * @author javawithease
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.

Related Topics:

How to build java project using ant in eclipse?
JAXB marshalling – convert java object to xml example.
How to create pdf file in java using iText jar?
Generics class example.
OGNL in struts 2.
Hibernate One-to-One Mapping using xml.
Send inline image in email using JavaMail API.
Quartz 2 JobListener example.


Comments are closed.