Saturday 23 February 2019

                                          Marshalling And UnMarshalling Process in java?
====================================================================================================
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.
==============================================================
Explanation:Marshalling
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
Create a JAXBContext instance
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);
Explanation:-UnMarshalling
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();
Creating Java Object with the help of unmarshaller reference by calling unmarshal() method.
Employee unmarshalledEmployee =(Employee)unmarshaller.unmarshal(new File(“employee.xml”));
package govind;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class Employee {
private int id;
private String name;
private Float salary;
@XmlAttribute
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@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;
}
}
==============================================================================================================================
package govind;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
public class EmployeeObjectToXml {
public static void main(String[] args) throws JAXBException, FileNotFoundException {
JAXBContext jaxbcontextobj = JAXBContext.newInstance(Employee.class);
Marshaller marshalobj = jaxbcontextobj.createMarshaller();
marshalobj.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,true);
Employee e1=new Employee();
e1.setId(1);
e1.setName("Govind Ballabh Khan");
e1.setSalary(10000.00f);
marshalobj.marshal(e1, new FileOutputStream("employee.xml"));
}
}
================================================================================================================================
package govind;
import java.io.File;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
public class EmployeeXmlToObject {
public static void main(String[] args) {
try
{
File f=new File("employee.xml");
JAXBContext jaxbcontext=JAXBContext.newInstance(Employee.class);
Unmarshaller unmarshalobj = jaxbcontext.createUnmarshaller();
Employee e1 = (Employee)unmarshalobj.unmarshal(f);
System.out.println("Id="+e1.getId()+" Name"+e1.getName()+" Salary="+e1.getSalary());
}
catch (JAXBException e) {
e.printStackTrace();
}
}

}
o/p:-----------------
Marshalling
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<employee id="1">
<name>Govind Ballabh Khan</name>
<salary>10000.0</salary>
</employee>
Unmarshalling
Id=1 NameGovind Ballabh Khan Salary=10000.0

No comments:

Post a Comment