<div dir="ltr" style="text-align: justify;">
<div dir="ltr" style="text-align: justify;">
<div dir="ltr" style="text-align: justify;">
<div dir="ltr" style="text-align: justify;">
<p>JAXB, stands for Java Architecture for XML Binding, using JAXB annotation to convert Java object to / from XML file. In this tutorial, we show you how to use JAXB to do following stuffs :</p>
<ul style="text-align: left;">
<li><b>Marshalling </b>– Convert a Java object into a XML file.</li>
<li><b>Unmarshalling </b>– Convert XML content into a Java Object.</li>
</ul>
<h3><b>Technologies used in this article</b></h3>
<ul style="text-align: left;">
<li>JDK 1.6</li>
<li>JAXB 2.2.7</li>
</ul>
<h2>JAXB Hello World Example</h2>
<p>Working with JAXB is easy, just annotate object with JAXB annotation, later use <i><b>jaxbMarshaller.marshal() </b></i>or <b><i>jaxbMarshaller.unmarshal()</i></b> to do the object / XML conversion.</p>
<p><b>Simple JAXB Marshalling Example: Converting Object into XML</b></p>
<p>Let&#8217;s see the steps to convert java object into XML document.</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 Marshaller objects</li>
<li>Create the content tree by using set methods</li>
<li>Call the marshal method</li>
</ol>
<p><b>Employee.java</b></p>
<pre class="highlight">package com.doj.jaxb; 
 
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 String 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 String getAddress() { 
 return address; 
 } 
 public void setAddress(String address) { 
 this.address = address; 
 } 
 
 
} 
</pre>
<p> ;</p>
<ul style="text-align: left;">
<li><b>@XmlRootElement </b>specifies the root element for the xml document.</li>
<li><b>@XmlAttribute </b>specifies the attribute for the root element.</li>
<li><b>@XmlElement</b> specifies the sub element for the root element.</li>
</ul>
<p> ;</p>
<pre class="highlight">package com.doj.jaxb; 
 
import java.io.FileNotFoundException; 
import java.io.FileOutputStream; 
 
import javax.xml.bind.JAXBContext; 
import javax.xml.bind.JAXBException; 
import javax.xml.bind.Marshaller; 
 
/** 
 * @author Dinesh Rajput 
 * 
 */ 
public class ObjectToXMLTest { 
 
 /** 
 * @param args 
 * @throws JAXBException 
 * @throws FileNotFoundException 
 */ 
 public static void main(String[] args) throws JAXBException, FileNotFoundException { 
 JAXBContext contextObj = JAXBContext.newInstance(Employee.class); 
 
 Marshaller marshallerObj = contextObj.createMarshaller(); 
 marshallerObj.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); 
 Employee emp = new Employee(); 
 emp.setEmpid(1111); 
 emp.setName("Dinesh Rajput"); 
 emp.setSalary(100000); 
 emp.setAddress("Noida"); 
 
 marshallerObj.marshal(emp, new FileOutputStream("employee.xml")); 
 } 
 
} 
</pre>
<p>Run this application as java program lets see following output file in application<br />
<b>employee.xml</b></p>
<pre class="highlight"><;?xml version="1.0" encoding="UTF-8" standalone="yes"?>; 
<;employee empid="1111">; 
 <;address>;Noida<;/address>; 
 <;name>;Dinesh Rajput<;/name>; 
 <;salary>;100000.0<;/salary>; 
<;/employee>; 
 
</pre>
</div>
<div class="separator" style="clear: both; text-align: center;"><img src="https://dineshonjava.com/wp-content/uploads/2014/02/111-jaxb-2.gif" border="0" /></div>
<p> ;</p>
</div>
<h3><b>Download Source Code with libs</b><br />
<b><a href="https://sites.google.com/a/dineshonjava.com/dineshonjava/dineshonjava/JaxBExample.zip?attredirects=0&;d=1" target="_blank" rel="noopener">JaxBExample.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-2x-tutorial/">Previous</a> <;<; || <a href="https://dineshonjava.com/jaxb-2x-tutorial/">Index </a>|| >;>;<a href="https://dineshonjava.com/jaxb-marshalling-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-2x-tutorial/">Previous</a> 
									 </div> 
									 <div class="wp-post-navigation-next"> 
									 <a href="https://dineshonjava.com/jaxb-marshalling-example/">Next</a> 
									 </div> 
									</div>
<script type="text/javascript">
jQuery(document).ready(function($) {
 $.post('https://dineshonjava.com/wp-admin/admin-ajax.php', {action: 'mts_view_count', id: '229'});
});
</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…