javax.xml.bind.JAXB.marshal() Method



Description

The Javax.xml.bind.JAXB.marshal(Object jaxbObject, Writer xml) method writes a Java object tree to XML and store it to the specified location.

Declaration

Following is the declaration for javax.xml.bind.JAXB.marshal(Object jaxbObject, Writer xml) method

public static void marshal(Object jaxbObject, Writer xml)

Parameters

  • jaxbObject − The Java object to be marshalled into XML. If this object is a JAXBElement, it will provide the root tag name and the body. If this object has XmlRootElement on its class definition, that will be used as the root tag name and the given object will provide the body. Otherwise, the root tag name is infered from the short class name. This parameter must not be null.

  • xml − The XML will be sent as a character stream to the given Writer. Upon a successful completion, the stream will be closed by this method.

Return Value

The method doesn't return any value.

Exception

  • DataBindingException − If the operation fails, such as due to I/O error, unbindable classes.

Example

The following example shows the usage of javax.xml.bind.JAXB.marshal(Object jaxbObject, Writer xml) 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;
 
@XmlRootElement
public class Student{
 
   String name;
   int age;
   int id;

   public String getName(){
      return name;
   }

   @XmlElement
   public void setName(String name){
      this.name = name;
   }

   public int getAge(){
      return age;
   }

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

   public int getId(){
      return id;
   }

   @XmlAttribute
   public void setId(int id){
      this.id = id;
   }
}

Now let us create main class which will be used to marshal ie. convert Student object into an XML file. This example marshals the 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 java.io.FileWriter;

import javax.xml.bind.JAXB;
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 javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;

public class JAXBDemo {
   public static void main(String[] args) {
      // create student object
      Student st = new Student();
      st.setAge(14);
      st.setName("Soniya");

      try {         
         // create file writer object
         FileWriter fw = new FileWriter("Student.xml");
         
         // marshal object to file writer
         JAXB.marshal(st, fw);
         
         // create document object from the student.xml
         DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
         DocumentBuilder docBuilder = dbf.newDocumentBuilder();
         Document document = docBuilder.parse("Student.xml");
                           
         // print the marshalled object to the stdout
         TransformerFactory tf = TransformerFactory.newInstance();
         Transformer t = tf.newTransformer();
         t.transform(new DOMSource(document), new StreamResult(System.out));
         
      }catch(Exception ex) {
         ex.printStackTrace();
      }
   }
}

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

To create document, an XML file is needed as input. The XML file is named as Student.xml

<?xml version = "1.0" encoding = "UTF-8" standalone = "no"?>
<root>
   <student id = "10">
      <age>14</age>
      <name>Soniya</name>
   </student>
</root>
javax_xml_bind_jaxb.htm
Advertisements