JAXB stands for Java Architecture for XML Binding. JAXB provides API to access and process a XML document. We can read or write XML files using JAXB. In this post, I am going to use JAXB annotation to convert Java Object to/ from XML file.
Before writing this post I read about marshalling and unmarshalling and found that Marshalling and Serialization in Java are loosely synonyms. Infact Serialization is part of Marshalling. Term Marshalling is used when we talk about passing objects to remote Objects(RMI). In Marshalling Object is serialized(Member data is Serialized) + codebase is attached. CodesBase is information that tells the receiver of Object where the implementation of this object can be found. Any program that thinks it might ever pass an object to another program that may not have seen it before must set the codebase, so that the receiver can know where to download the code from, if it doesn't have the code available locally. The receiver will, upon deserializing the object, fetch the codebase from it and load the code from that location.
Marshalling - Convert a Java Object into an XML.
Unmarshalling - Convert an XML content into a Java Object
Class JAXBExample for Marshalling and UnMarshalling
public class JAXBExample { private JAXBContext jaxbContext; public static void main(String args[]) { JAXBExample jaxb = new JAXBExample(); Employee employee = new Employee("1002", "Ankit", 200000, "Bangalore", 9876); jaxb.convertToXML(employee); jaxb.convertToObject(); } /** * Marshallling * * @param employee */ private void convertToXML(Employee employee) { try { jaxbContext = JAXBContext.newInstance(Employee.class); // Obtain marshaller from JaxbContext Marshaller marshaller = jaxbContext.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); marshaller.marshal(employee, System.out); } catch (JAXBException e) { e.printStackTrace(); } } /** * Unmarshalling */ private void convertToObject() { Unmarshaller jaxbUnmarshaller; try { jaxbUnmarshaller = jaxbContext.createUnmarshaller(); Employee employee = (Employee) jaxbUnmarshaller.unmarshal(new File( "D:\\test.xml")); System.out.println("\n"+"Employee Details:"); System.out.println("Id:" + employee.getId() + "\n" + "Name:" + employee.getName() + "\n" + "Salary:" + employee.getSalary() + "\n" + "Address:" + employee.getAddress() + "\n" + "Contact:" + employee.getContact()); } catch (JAXBException e) { e.printStackTrace(); } } }
Annotated Class Employee
@XmlRootElement class Employee { private String id; private String name; private int salary; private String address; private int contact; Employee() { } Employee(String id, String name, int salary, String address, int contact) { this.id = id; this.name = name; this.salary = salary; this.address = address; this.contact = contact; } public String getId() { return id; } @XmlAttribute public void setId(String id) { this.id = id; } public String getName() { return name; } @XmlElement public void setName(String name) { this.name = name; } public int getSalary() { return salary; } @XmlElement public void setSalary(int salary) { this.salary = salary; } public String getAddress() { return address; } @XmlElement public void setAddress(String address) { this.address = address; } public int getContact() { return contact; } @XmlElement public void setContact(int contact) { this.contact = contact; } }
XML
<?xml version="1.0"?> <employee id="1002"> <name>Ankit</name> <salary>200000</salary> <address>Bangalore</address> <contact>9876</contact> </employee>
Output
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<employee id="1002">
<address>Bangalore</address>
<contact>9876</contact>
<name>Ankit</name>
<salary>200000</salary>
</employee>
Employee Details:
Id:1002
Name:Ankit
Salary:200000
Address:Bangalore
Contact:9876
Great going..
ReplyDeleteThanks!! More to come..Stay tuned.. :)
DeleteThanks Ankit for sharing. Looking forward for more.
ReplyDelete