<div dir="ltr" style="text-align: left;">
<div dir="ltr" style="text-align: justify;">
<p>By the help of <i><b>UnMarshaller </b></i>interface, we can unmarshal(read) the object into xml document.In this example, we are going to convert simple xml document into java object.</p>
<p>Let&#8217;s see the steps to convert XML document into java object.</p>
<ol style="text-align: left;">
<li>Create POJO or bind the schema and generate the classes</li>
<li>Create the JAXBContext object</li>
<li>Create the Unmarshaller objects</li>
<li>Call the unmarshal method</li>
<li>Use getter methods of POJO to access the data</li>
</ol>
<h2><b>Unmarshaller example: Converting xml document into java object</b><br />
<b></b></h2>
<p><b>Xml Document(employee.xml)</b></p>
<pre class="highlight"><;?xml version="1.0" encoding="UTF-8" standalone="yes"?>; 
<;employee empid="1111">; 
 <;address>; 
 <;city>;Noida<;/city>; 
 <;locality>;Sector-88<;/locality>; 
 <;pin>;201301<;/pin>; 
 <;/address>; 
 <;address>; 
 <;city>;Kannauj<;/city>; 
 <;locality>;Kamala Nagar<;/locality>; 
 <;pin>;209724<;/pin>; 
 <;/address>; 
 <;name>;Dinesh Rajput<;/name>; 
 <;salary>;100000.0<;/salary>; 
<;/employee>; 
</pre>
<p><b>Employee.java</b></p>
<pre class="highlight">package com.doj.jaxb; 
 
import java.util.List; 
 
import javax.xml.bind.annotation.XmlAttribute; 
import javax.xml.bind.annotation.XmlElement; 
import javax.xml.bind.annotation.XmlRootElement; 
 
/** 
 * @author Dinesh Rajput 
 * 
 */ 
@XmlRootElement 
public class Employee { 
 private int empid; 
 private String name; 
 private float salary; 
 private List<;Address>; address; 
 
 @XmlAttribute 
 public int getEmpid() { 
 return empid; 
 } 
 public void setEmpid(int empid) { 
 this.empid = empid; 
 } 
 @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; 
 } 
 @XmlElement 
 public List<;Address>; getAddress() { 
 return address; 
 } 
 public void setAddress(List<;Address>; address) { 
 this.address = address; 
 } 
 
} 
 
</pre>
<p><b>Address.java</b></p>
<pre class="highlight">package com.doj.jaxb; 
 
/** 
 * @author Dinesh Rajput 
 * 
 */ 
public class Address { 
 private String locality; 
 private String city; 
 private int pin; 
 
 public String getLocality() { 
 return locality; 
 } 
 public void setLocality(String locality) { 
 this.locality = locality; 
 } 
 public String getCity() { 
 return city; 
 } 
 public void setCity(String city) { 
 this.city = city; 
 } 
 public int getPin() { 
 return pin; 
 } 
 public void setPin(int pin) { 
 this.pin = pin; 
 } 
} 
</pre>
<p><b>JaxbUnmarshalTest.java</b></p>
<pre class="highlight">package com.doj.jaxb; 
import java.io.File; 
import java.util.List; 
 
import javax.xml.bind.JAXBContext; 
import javax.xml.bind.JAXBException; 
import javax.xml.bind.Unmarshaller; 
 
/** 
 * @author Dinesh Rajput 
 * 
 */ 
public class JaxbUnmarshalTest { 
 
 /** 
 * @param args 
 * @throws JAXBException 
 */ 
 public static void main(String[] args) throws JAXBException { 
 File file = new File("employee.xml"); 
 JAXBContext jaxbContext = JAXBContext.newInstance(Employee.class); 
 
 Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller(); 
 Employee emp = (Employee) jaxbUnmarshaller.unmarshal(file); 
 
 System.out.println("Employee-"+emp.getEmpid()+" | "+emp.getName()+" | "+emp.getSalary()); 
 System.out.println("Addresses: "); 
 List<;Address>; list = emp.getAddress(); 
 for(Address address : list){ 
 System.out.println(address.getLocality()+" | "+address.getCity()+" | "+address.getPin()); 
 } 
 } 
} 
</pre>
<p><b>Run this application you get following output-</b><br />
<i>Employee-1111 | Dinesh Rajput | 100000.0</i><br />
<i>Addresses: </i><br />
<i>Sector-88 | Noida | 201301</i><br />
<i>Kamala Nagar | Kannauj | 209724</i></p>
<p> ;</p>
<div class="separator" style="clear: both; text-align: center;"><img src="https://dineshonjava.com/wp-content/uploads/2014/02/111-jaxb.gif" border="0" /></div>
<h3><b>Download Source Code</b><br />
<b><a href="https://sites.google.com/a/dineshonjava.com/dineshonjava/dineshonjava/JaxBUnmarshalExample.zip?attredirects=0&;d=1" target="_blank" rel="noopener">JaxBUnmarshalExample.zip</a></b></h3>
</div>
<p> ;</p>
<div style="background-color: pink; border-width: thin; text-align: center;"><b><;<;<a href="https://dineshonjava.com/jaxb-marshalling-example/">Previous</a> <;<; || <a href="https://dineshonjava.com/jaxb-2x-tutorial/">Index </a>|| >;>;<a href="https://dineshonjava.com/jaxb-unmarshalling-example/" target="_blank" rel="noopener">Next</a> >;>;</b></div>
<p> ;</p>
</div>
<div class="wp-post-navigation"> 
									 <div class="wp-post-navigation-pre"> 
									 <a href="https://dineshonjava.com/jaxb-marshalling-example/">Previous</a> 
									 </div> 
									 <div class="wp-post-navigation-next"> 
									 
									 </div> 
									</div>
<script type="text/javascript">
jQuery(document).ready(function($) {
 $.post('https://dineshonjava.com/wp-admin/admin-ajax.php', {action: 'mts_view_count', id: '227'});
});
</script>
Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…
Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…
Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…
Technology has emerged a lot in the last decade, and now we have artificial intelligence;…
Managing a database is becoming increasingly complex now due to the vast amount of data…
Overview In this article, we will explore Spring Scheduler how we could use it by…