Javax.xml.bind.JAXBContext.createMarshaller() Method



The Javax.xml.bind.JAXBContext.createMarshaller() create a Marshaller object that can be used to convert a java content tree into XML data.


Following is the declaration for javax.xml.bind.JAXBContext.createMarshaller() method

public abstract Marshaller createMarshaller()


  • NA

Return Value

This method returns a marshaller object


  • JAXBException -- if an error was encountered while creating the Marshaller object.


The following example shows the usage of javax.xml.bind.JAXBContext.createMarshaller() method. To proceed, consider the following Student class which will be used to have objects for marshalling purpose:

package com.tutorialspoint;

import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
public class Student{
   String name;
   int age;
   int id;

   public String getName(){
      return name;

   public void setName(String name){ = name;

   public int getAge(){
      return age;

   public void setAge(int age){
      this.age = age;

   public int getId(){
      return id;

   public void setId(int id){ = id;

Now let us create main class which will be used to marshal ie. convert Student object into an XML file. This example creates marshaller object to marshals Student object and prints it at STDOUT, but in practical scenario you can store the object in any file as an XML node.

package com.tutorialspoint;


import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;

import org.w3c.dom.Document;

public class JAXBContextDemo {
   public static void main(String[] args) {
      // create a Student object and set its properties
      Student student = new Student();

      try {
         // we need a blank document to store final xml output
         DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
         DocumentBuilder docBuilder = dbf.newDocumentBuilder();
         Document document = docBuilder.newDocument();
         // create JAXBContext which will be used to create a Binder
         JAXBContext jc = JAXBContext.newInstance(Student.class);
         // create new file
         File f = new File("Student.xml");
         // create marshaller object
         Marshaller m = jc.createMarshaller();
         // marshal java content tree to the XML
         m.marshal(student, f);
         // finally print the marshalled object on stdout
         TransformerFactory tf = TransformerFactory.newInstance();
         Transformer t = tf.newTransformer();
         t.transform(new DOMSource(document), new StreamResult(System.out));
      }catch(Exception ex) {

Before we proceed for compilation, we need to make sure that that we download and put it in our CLASSPATH. Once setup is ready, let us compile and run the above program, this will produce the following result:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<student id="10">