Convert XML to Object using JAXB
In this tutorial, I am attaching simple program about, How to Convert XML to Object using JAXB. The example program has been shared in the same post.
Note : The conversion of XML to Object typically called as Unmarshalling
This example program has been tested with environment and output are shared in the same post. This example deals with following three files such as
- employee.xml
- Employee.java
- XMLToObject.java
All these files were attached in the post with explanation.
employee.xml
This is the input XML file. eventually we going to Convert XML to Object using JAXB, Please make sure that elements name and Object attributes are same.
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <employee> <employeeAddress>India</employeeAddress> <employeeAge>25</employeeAge> <employeeGender>Male</employeeGender> <employeeId>1001</employeeId> <employeeName>Dinesh Krishnan</employeeName> </employee>
Employee.java
The Employee.java is normal POJO(Plain Old Java Object) class. Which has attributes, getters and setters accordingly. In order convert XML to Object we have to use @XmlRootElement annotation on top of the class. The following code will give you idea about @XmlRootElement annotation usage.
package com.dineshkrish.jaxb; import javax.xml.bind.annotation.XmlRootElement; @XmlRootElement public class Employee { private int employeeId; private String employeeName; private int employeeAge; private String employeeGender; private String employeeAddress; public int getemployeeId() { return employeeId; } public void setemployeeId(int employeeId) { this.employeeId = employeeId; } public String getemployeeName() { return employeeName; } public void setemployeeName(String employeeName) { this.employeeName = employeeName; } public int getemployeeAge() { return employeeAge; } public void setemployeeAge(int employeeAge) { this.employeeAge = employeeAge; } public String getemployeeGender() { return employeeGender; } public void setemployeeGender(String employeeGender) { this.employeeGender = employeeGender; } public String getemployeeAddress() { return employeeAddress; } public void setemployeeAddress(String employeeAddress) { this.employeeAddress = employeeAddress; } }
XMLToObject.java
This is the class which is responsible to convert XML to Object using JAXB. Here we have to use JAXB API`s classes such as JAXBContext and Unmarshaller to perform the conversion. The following example will give you idea about it.
package com.dineshkrish.jaxb; import java.io.File; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; import javax.xml.bind.Unmarshaller; public class XMLToObject { public static void main(String[] args) { Employee employee = null; try { // Creating the file object for XML file File file = new File("employee.xml"); // Defining JAXBContext Object JAXBContext context = JAXBContext.newInstance(Employee.class); Unmarshaller umUnmarshaller = context.createUnmarshaller(); // Converting XML to Object using unmarshal() method employee = (Employee)umUnmarshaller.unmarshal(file); System.out.println("XML to Object Conversion done Successfully..."); System.out.println("----------------------------------------------"); // Accessing the attributes from object (ie: after conversion) System.out.println("Employee Id : "+employee.getemployeeId()); System.out.println("Employee Name : "+employee.getemployeeName()); System.out.println("Employee Age : "+employee.getemployeeAge()); System.out.println("Employee Gender : "+employee.getemployeeGender()); System.out.println("Employee Address : "+employee.getemployeeAddress()); } catch (JAXBException e) { System.out.println(e.getMessage()); e.printStackTrace(); } } }
Output
—————–
XML to Object Conversion done Successfully…
———————————————-
Employee Id : 1001
Employee Name : Dinesh Krishnan
Employee Age : 25
Employee Gender : Male
Employee Address : India
References
More from my site

Hello, folks, I am a founder of idineshkrishnan.com. I love open source technologies, If you find my tutorials are useful, please consider making donations to these charities.
No responses yet