JAXB marshalling – convert java object to xml example using multiple 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:

Below example have two pojo classes Student and Subject. A Student can have one or more Subject. Student class is annotated with @XmlRootElement annotation, so Student will be the root element of the xml. Setter methods of name, className, rollNo and subject properties are annotated with @XmlElement annotation, so these properties will be the elements of xml. As subject is also a pojo, so its properties will be automatically processed in the marshalling process. 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.

Example:

Student.java

import java.util.List;
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 w3schools
 */
@XmlRootElement
public class Student {	
	private String name;
	private String rollNo;
	private String className;
	private int age;
	private int id;
	private List<Subject> subject;
 
	//Default constructor
	public Student(){
 
	}
 
	//Parameterised constructor
	public Student(String name, String rollNo, 
			String className, int age, 
                        int id, List<Subject> subject){
		this.name = name;
		this.rollNo = rollNo;
		this.className = className;
		this.age = age;
		this.id = id;
		this.subject = subject;		
	}	
 
	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;
	}
 
	public List<Subject> getSubject() {
		return subject;
	}
 
	@XmlElement
	public void setSubject(List<Subject> subject) {
		this.subject = subject;
	}
 
}

Subject.java

/**
 * This class represents Address.
 * @author w3schools
 */
public class Subject {	
	private String subjectname;
	private String subjectId;
 
 
	//Default constructor
	public Subject(){
 
	}
 
	//Parameterised constructor
	public Subject(String subjectname, String subjectId){
		this.subjectname = subjectname;
		this.subjectId = subjectId;
	}
 
	public String getSubjectname() {
		return subjectname;
	}
 
	public void setSubjectname(String subjectname) {
		this.subjectname = subjectname;
	}
 
	public String getSubjectId() {
		return subjectId;
	}
 
	public void setSubjectId(String subjectId) {
		this.subjectId = subjectId;
	}
 
}

JAXBTest.java

import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
 
/**
 * This is test class for Marshalling.
 * @author w3schools
 */
public class JAXBTest {
  public static void main(String args[]){   
    try {
	//Create List of Address
	List<Subject> subject = new ArrayList<Subject>();
	subject.add(new Subject("Java", "1"));
	subject.add(new Subject("Oracle", "2"));
 
	//Create Student object
	Student student = 
           new Student("jai", "MCA/07/06", "MCA final", 27, 6, subject);
 
	//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 unmarshalling – convert xml into java object example.
Previous Topic: JAXB marshalling – convert java object to xml example using one pojo.