Wednesday, July 22, 2020

JAXB Marshalling & Unmarshallling Example



In this Introduction of JAXB tutorial, we are going to see about JAXB annotation and concepts. JAXB means Java Architecture for XML Binding. This JAXB API makes developers life easy for converting “XML Document to Java Object” (Marshalling) and  “Java Object to an XML document” (Unmarshalling). Before JAXB came into picture, developers might use different parsers (like SAX Parser) to parse entire XML document by reading all the elements from top to bottom along with the xml attributes and finally form an Java Object. But when JAXB came, it really saves lot of time for parsing the XML Document.
Before going to see about JAXB annotation, we will first see the concepts behind this JAXB annotation. JAXB basically works on the principle of Marshalling and Unmarshalling.
Marshalling – The process of converting Java Object into XML document or JSON Object.
Unmarshalling – The process of reconverting the XML document or JSON object into Java Object.
Before discussing about different annotations in JAXB API in this blog we will see the basic example of JAXB  – Marshalling and Unmarshalling.

Example of JAXB Marshalling:

Employee.java
package com.learnfromexamples.jaxb;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class Employee {
    private int empNumber;
    private String empName;
    private String empAddres;
    public int getEmpNumber() {
        return empNumber;
    }
    public void setEmpNumber(int empNumber) {
        this.empNumber = empNumber;
    }
    public String getEmpName() {
        return empName;
    }
    public void setEmpName(String empName) {
        this.empName = empName;
    }
    public String getEmpAddres() {
        return empAddres;
    }
    public void setEmpAddres(String empAddres) {
        this.empAddres = empAddres;
    }
}
Explanation:
As I told u earlier, to convert Java Object into an XML Document we are going to use JAXB API. Let’s see the steps to convert Java Object to XML (Marshalling using JAXB API).
  • Annotate the Java class with
    @XmlRootElement annotation (Employee.java).
  • Create a JAXBContext instance (JAXBMarshallingImpl.java)
JAXBContext context = JAXBContext.newInstance(Employee.class);
  • Create a Marshaller reference with the help of JAXBContext instance by calling createMarshaller() method.
Marshaller marshaller = context.createMarshaller();
  • Creating XML object with the help of marshaller reference by calling marshal() method.
marshaller.marshal(employee, System.out);

JAXBMarshallingImpl.java
package com.learnfromexamples.jaxb.impl;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import com.learnfromexamples.jaxb.Employee;
public class JAXBMarshallingImpl {
    public static void main(String[] args) {
        Employee employee = new Employee();
        employee.setEmpNumber(01);
        employee.setEmpName("Sridhar");
        employee.setEmpAddres("Chennai");
        try {
            JAXBContext context =
                    JAXBContext.newInstance(Employee.class);
            Marshaller marshaller =
                    context.createMarshaller();
            marshaller.setProperty(marshaller.JAXB_FORMATTED_OUTPUT, true);
            marshaller.marshal(employee, System.out);
        } catch (JAXBException e) {
            e.printStackTrace();
        }
         
    }
}
JAXB- Marshalling Output
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<employee>
    <empAddres>Chennai</empAddres>
    <empName>Sridhar</empName>
    <empNumber>1</empNumber>
</employee>

Example of JAXB Unmarshalling

employee.xml
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<employee>
    <empAddres>Chennai</empAddres>
    <empName>Sridhar</empName>
    <empNumber>1</empNumber>
</employee>

JAXBUnmarshallingImpl.java
package com.learnfromexamples.jaxb.impl;
import java.io.File;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import com.learnfromexamples.jaxb.Employee;
public class JAXBUnmarshallingImpl {
    public static void main(String[] args) {
        try {
            JAXBContext context = JAXBContext.newInstance(Employee.class);
            Unmarshaller unmarshaller =
                    context.createUnmarshaller();
            Employee unmarshalledEmployee =
                    (Employee)unmarshaller.unmarshal(new File("employee.xml"));
            System.out.println("Employee Number is : "
                    +unmarshalledEmployee.getEmpNumber());
            System.out.println("Employee Name is : "
                    +unmarshalledEmployee.getEmpName());
            System.out.println("Employee Address is : "
                    +unmarshalledEmployee.getEmpAddres());
        } catch (JAXBException e) {
            e.printStackTrace();
        }
         
    }
}

JAXB – Unmarshalling Output
Employee Number is : 1
Employee Name is : Sridhar
Employee Address is : Chennai

Explanation:
Let’s see the steps to convert XML Object to Java (Unmarshalling using JAXB API)
  • Create a JAXBContext instance
JAXBContext context = JAXBContext.newInstance(Employee.class);
  • Create a Unmarshaller reference with the help of JAXBContext instance by calling createUnmarshaller() method.
Unmarshaller unmarshaller = context.createUnmarshaller();
  1. Creating Java Object with the help of unmarshaller reference by calling unmarshal() method.
Employee unmarshalledEmployee =
(Employee)unmarshaller.unmarshal(new File(“employee.xml”));
 So far what we have seen is just a introduction of JAXB API tutorial, we are going to see in detail about some of the important JAXB annotations along with examples in the forthcoming blog.
If you feel this blog is useful and informative, please share this blog to your friends and colleagues on Facebook, Twitter, Google Plus page.



Reference :


Previous Post
Next Post

0 komentar: