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.

Steps:

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.

Example:

Student.java

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 w3spoint
 */
@XmlRootElement
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;
	}
 
	@XmlElement
	public void setName(String name) {
		this.name = name;
	}
 
	public String getClassName() {
		return className;
	}
 
	@XmlElement
	public void setClassName(String className) {
		this.className = className;
	}
 
	public String getRollNo() {
		return rollNo;
	}
 
	@XmlElement
	public void setRollNo(String rollNo) {
		this.rollNo = rollNo;
	}
 
	public int getAge() {
		return age;
	}
 
	@XmlTransient
	public void setAge(int age) {
		this.age = age;
	}
 
	public int getId() {
		return id;
	}
 
	@XmlAttribute
	public void setId(int id) {
		this.id = id;
	}	
}

JAXBTest.java

import java.io.FileOutputStream;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
 
/**
 * This is test class for Marshalling.
 * @author w3spoint
 */
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
	marshaller.marshal(student, 
                            new FileOutputStream("D:\\Student.xml"));
	System.out.println("java object converted to xml successfully.");
     } catch (Exception e) {
		e.printStackTrace();
     }	   
  }
}

Output:

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.

 

Content Protection by DMCA.com
Please Share