JAXB Marshalling Example

In this tutorial we are using Marshaller interface, we can marshal(write) the object into xml document. In the previous page, we have seen the simple example of converting object into xml.

JAXB Marshalling Example

In this example, we are going to convert the object into xml having primitives, strings and collection objects.

Let’s see the steps to convert java object into XML document.

  1. Create POJO or bind the schema and generate the classes
  2. Create the JAXBContext object
  3. Create the Marshaller objects
  4. Create the content tree by using set methods
  5. Call the marshal method

Employee.java

package com.doj.jaxb;

import java.util.List;

import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

/**
 * @author Dinesh Rajput
 *
 */
@XmlRootElement
public class Employee {
 private int empid;  
    private String name;  
    private float salary;
    private List<Address> address;
 
    @XmlAttribute  
    public int getEmpid() {
  return empid;
 }
 public void setEmpid(int empid) {
  this.empid = empid;
 }
 @XmlElement  
 public String getName() {
  return name;
 }
 public void setName(String name) {
  this.name = name;
 }
 @XmlElement  
 public float getSalary() {
  return salary;
 }
 public void setSalary(float salary) {
  this.salary = salary;
 }
 @XmlElement  
 public List<Address> getAddress() {
  return address;
 }
 public void setAddress(List<Address> address) {
  this.address = address;
 }
    
}

Address.java

package com.doj.jaxb;

/**
 * @author Dinesh Rajput
 *
 */
public class Address {
 private String locality;
 private String city;
 private int pin;
 
 public String getLocality() {
  return locality;
 }
 public void setLocality(String locality) {
  this.locality = locality;
 }
 public String getCity() {
  return city;
 }
 public void setCity(String city) {
  this.city = city;
 }
 public int getPin() {
  return pin;
 }
 public void setPin(int pin) {
  this.pin = pin;
 }
}

JaxBMarshalTest.java

package com.doj.jaxb;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;

/**
 * @author Dinesh Rajput
 *
 */
public class JaxBMarshalTest {

 /**
  * @param args
  * @throws JAXBException 
  * @throws FileNotFoundException 
  */
 public static void main(String[] args) throws JAXBException, FileNotFoundException {
  JAXBContext contextObj = JAXBContext.newInstance(Employee.class);  
    
     Marshaller marshallerObj = contextObj.createMarshaller();  
     marshallerObj.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);  
   
     Address localAddress = new Address();
     localAddress.setLocality("Sector-88");
     localAddress.setCity("Noida");
     localAddress.setPin(201301);
     
     Address permanentAddress = new Address();  
     permanentAddress.setLocality("Kamala Nagar");
     permanentAddress.setCity("Kannauj");
     permanentAddress.setPin(209724);
     
     List<Address> list=new ArrayList<Address>();  
     list.add(localAddress);  
     list.add(permanentAddress);  
       
     Employee emp = new Employee();
     emp.setEmpid(1111);
     emp.setName("Dinesh Rajput");
     emp.setSalary(100000);
     emp.setAddress(list);
     
     marshallerObj.marshal(emp, new FileOutputStream("employee.xml"));  
 }

}

Now run this application we get the following employee.xml file.

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<employee empid="1111">
    <address>
        <city>Noida</city>
        <locality>Sector-88</locality>
        <pin>201301</pin>
    </address>
    <address>
        <city>Kannauj</city>
        <locality>Kamala Nagar</locality>
        <pin>209724</pin>
    </address>
    <name>Dinesh Rajput</name>
    <salary>100000.0</salary>
</employee>

 

JAXB Marshalling Example

Download Source Code
JaxBMarshalExample.zip

 

<<Previous <<   || Index ||   >>Next >>

 

Previous
Next