JAXB example for Marshalling the java object to xml

First of all we need to create a JAXB object. With 4 fields along with getters and setters we are going to create an Employee object.

package com.java.example;

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

@XmlRootElement
public class Employee {

    private String firstName;
    private String lastName;
    private long ssn;
    private int eId;

    @XmlElement
    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    @XmlElement
    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    @XmlElement
    public long getSsn() {
        return ssn;
    }

    public void setSsn(long ssn) {
        this.ssn = ssn;
    }

    @XmlAttribute
    public int geteId() {
        return eId;
    }

    public void seteId(int eId) {
        this.eId = eId;
    }
}


Now, we are going to create an EmpObj class and in which we are going to set the value for Employee object. Once after we run this java class, we will be getting the employee.xml file generated into the specified temp folder and also the same content will be printed into the console.


package com.java.example;

import java.io.File;

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

public class EmpObj {

    public static void main(String[] args) {

        Employee employee = new Employee();
        employee.seteId(007);
        employee.setFirstName("James");
        employee.setLastName("Bond");
        employee.setSsn(123456789);

        try {

            File file = new File("C:\\Temp\\employee.xml");
            JAXBContext jaxbContext = JAXBContext.newInstance(Employee.class);
            Marshaller jaxbMarshaller = jaxbContext.createMarshaller();

            // optional line to format the xml, without this line the whole xml
            // will be printed into one single line
            jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

            // This line will write the generated xml into the specified temp
            // location
            jaxbMarshaller.marshal(employee, file);
            // This line will print the generated xml into the console.
            jaxbMarshaller.marshal(employee, System.out);

        } catch (JAXBException e) {
            e.printStackTrace();
        }
    }
}


Output:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<employee eId="7">
    <firstName>James</firstName>
    <lastName>Bond</lastName>
    <ssn>123456789</ssn>
</employee>

1 comment:

  1. I have read your blog its very attractive and impressive. I like it your blog.

    Java Training in Chennai Core Java Training in Chennai Core Java Training in Chennai

    Java Online Training Java Online Training JavaEE Training in Chennai Java EE Training in Chennai

    ReplyDelete