Categories: JAXB

JAXB Marshalling Example

<div dir&equals;"ltr" style&equals;"text-align&colon; justify&semi;">&NewLine;<div dir&equals;"ltr" style&equals;"text-align&colon; justify&semi;">&NewLine;<div dir&equals;"ltr" style&equals;"text-align&colon; justify&semi;">In this tutorial we are using Marshaller interface&comma; we can marshal&lpar;write&rpar; the object into xml document&period; In the previous page&comma; we have seen the simple example of converting object into xml&period;<&sol;p>&NewLine;<h2>JAXB Marshalling Example<&sol;h2>&NewLine;<p>In this example&comma; we are going to convert the object into xml having primitives&comma; strings and collection objects&period;<&sol;p>&NewLine;<p>Let&&num;8217&semi;s see the steps to convert java object into XML document&period;<&sol;p>&NewLine;<ol style&equals;"text-align&colon; left&semi;">&NewLine;<li>Create POJO or bind the schema and generate the classes<&sol;li>&NewLine;<li>Create the JAXBContext object<&sol;li>&NewLine;<li>Create the Marshaller objects<&sol;li>&NewLine;<li>Create the content tree by using set methods<&sol;li>&NewLine;<li>Call the marshal method<&sol;li>&NewLine;<&sol;ol>&NewLine;<div id&equals;"ads-id" align&equals;"center"><&sol;div>&NewLine;<p><b>Employee&period;java<&sol;b><&sol;p>&NewLine;<pre class&equals;"highlight">package com&period;doj&period;jaxb&semi; &NewLine; &NewLine;import java&period;util&period;List&semi; &NewLine; &NewLine;import javax&period;xml&period;bind&period;annotation&period;XmlAttribute&semi; &NewLine;import javax&period;xml&period;bind&period;annotation&period;XmlElement&semi; &NewLine;import javax&period;xml&period;bind&period;annotation&period;XmlRootElement&semi; &NewLine; &NewLine;&sol;&ast;&ast; &NewLine; &ast; &commat;author Dinesh Rajput &NewLine; &ast; &NewLine; &ast;&sol; &NewLine;&commat;XmlRootElement &NewLine;public class Employee &lbrace; &NewLine; private int empid&semi; &NewLine; private String name&semi; &NewLine; private float salary&semi; &NewLine; private List&lt&semi;Address&gt&semi; address&semi; &NewLine; &NewLine; &commat;XmlAttribute &NewLine; public int getEmpid&lpar;&rpar; &lbrace; &NewLine; return empid&semi; &NewLine; &rcub; &NewLine; public void setEmpid&lpar;int empid&rpar; &lbrace; &NewLine; this&period;empid &equals; empid&semi; &NewLine; &rcub; &NewLine; &commat;XmlElement &NewLine; public String getName&lpar;&rpar; &lbrace; &NewLine; return name&semi; &NewLine; &rcub; &NewLine; public void setName&lpar;String name&rpar; &lbrace; &NewLine; this&period;name &equals; name&semi; &NewLine; &rcub; &NewLine; &commat;XmlElement &NewLine; public float getSalary&lpar;&rpar; &lbrace; &NewLine; return salary&semi; &NewLine; &rcub; &NewLine; public void setSalary&lpar;float salary&rpar; &lbrace; &NewLine; this&period;salary &equals; salary&semi; &NewLine; &rcub; &NewLine; &commat;XmlElement &NewLine; public List&lt&semi;Address&gt&semi; getAddress&lpar;&rpar; &lbrace; &NewLine; return address&semi; &NewLine; &rcub; &NewLine; public void setAddress&lpar;List&lt&semi;Address&gt&semi; address&rpar; &lbrace; &NewLine; this&period;address &equals; address&semi; &NewLine; &rcub; &NewLine; &NewLine;&rcub; &NewLine;<&sol;pre>&NewLine;<p><b>Address&period;java<&sol;b><&sol;p>&NewLine;<pre class&equals;"highlight">package com&period;doj&period;jaxb&semi; &NewLine; &NewLine;&sol;&ast;&ast; &NewLine; &ast; &commat;author Dinesh Rajput &NewLine; &ast; &NewLine; &ast;&sol; &NewLine;public class Address &lbrace; &NewLine; private String locality&semi; &NewLine; private String city&semi; &NewLine; private int pin&semi; &NewLine; &NewLine; public String getLocality&lpar;&rpar; &lbrace; &NewLine; return locality&semi; &NewLine; &rcub; &NewLine; public void setLocality&lpar;String locality&rpar; &lbrace; &NewLine; this&period;locality &equals; locality&semi; &NewLine; &rcub; &NewLine; public String getCity&lpar;&rpar; &lbrace; &NewLine; return city&semi; &NewLine; &rcub; &NewLine; public void setCity&lpar;String city&rpar; &lbrace; &NewLine; this&period;city &equals; city&semi; &NewLine; &rcub; &NewLine; public int getPin&lpar;&rpar; &lbrace; &NewLine; return pin&semi; &NewLine; &rcub; &NewLine; public void setPin&lpar;int pin&rpar; &lbrace; &NewLine; this&period;pin &equals; pin&semi; &NewLine; &rcub; &NewLine;&rcub; &NewLine;<&sol;pre>&NewLine;<p><b>JaxBMarshalTest&period;java<&sol;b><&sol;p>&NewLine;<pre class&equals;"highlight">package com&period;doj&period;jaxb&semi; &NewLine; &NewLine;import java&period;io&period;FileNotFoundException&semi; &NewLine;import java&period;io&period;FileOutputStream&semi; &NewLine;import java&period;util&period;ArrayList&semi; &NewLine;import java&period;util&period;List&semi; &NewLine; &NewLine;import javax&period;xml&period;bind&period;JAXBContext&semi; &NewLine;import javax&period;xml&period;bind&period;JAXBException&semi; &NewLine;import javax&period;xml&period;bind&period;Marshaller&semi; &NewLine; &NewLine;&sol;&ast;&ast; &NewLine; &ast; &commat;author Dinesh Rajput &NewLine; &ast; &NewLine; &ast;&sol; &NewLine;public class JaxBMarshalTest &lbrace; &NewLine; &NewLine; &sol;&ast;&ast; &NewLine; &ast; &commat;param args &NewLine; &ast; &commat;throws JAXBException &NewLine; &ast; &commat;throws FileNotFoundException &NewLine; &ast;&sol; &NewLine; public static void main&lpar;String&lbrack;&rsqb; args&rpar; throws JAXBException&comma; FileNotFoundException &lbrace; &NewLine; JAXBContext contextObj &equals; JAXBContext&period;newInstance&lpar;Employee&period;class&rpar;&semi; &NewLine; &NewLine; Marshaller marshallerObj &equals; contextObj&period;createMarshaller&lpar;&rpar;&semi; &NewLine; marshallerObj&period;setProperty&lpar;Marshaller&period;JAXB&lowbar;FORMATTED&lowbar;OUTPUT&comma; true&rpar;&semi; &NewLine; &NewLine; Address localAddress &equals; new Address&lpar;&rpar;&semi; &NewLine; localAddress&period;setLocality&lpar;"Sector-88"&rpar;&semi; &NewLine; localAddress&period;setCity&lpar;"Noida"&rpar;&semi; &NewLine; localAddress&period;setPin&lpar;201301&rpar;&semi; &NewLine; &NewLine; Address permanentAddress &equals; new Address&lpar;&rpar;&semi; &NewLine; permanentAddress&period;setLocality&lpar;"Kamala Nagar"&rpar;&semi; &NewLine; permanentAddress&period;setCity&lpar;"Kannauj"&rpar;&semi; &NewLine; permanentAddress&period;setPin&lpar;209724&rpar;&semi; &NewLine; &NewLine; List&lt&semi;Address&gt&semi; list&equals;new ArrayList&lt&semi;Address&gt&semi;&lpar;&rpar;&semi; &NewLine; list&period;add&lpar;localAddress&rpar;&semi; &NewLine; list&period;add&lpar;permanentAddress&rpar;&semi; &NewLine; &NewLine; Employee emp &equals; new Employee&lpar;&rpar;&semi; &NewLine; emp&period;setEmpid&lpar;1111&rpar;&semi; &NewLine; emp&period;setName&lpar;"Dinesh Rajput"&rpar;&semi; &NewLine; emp&period;setSalary&lpar;100000&rpar;&semi; &NewLine; emp&period;setAddress&lpar;list&rpar;&semi; &NewLine; &NewLine; marshallerObj&period;marshal&lpar;emp&comma; new FileOutputStream&lpar;"employee&period;xml"&rpar;&rpar;&semi; &NewLine; &rcub; &NewLine; &NewLine;&rcub; &NewLine;<&sol;pre>&NewLine;<h2><b>Now run this application we get the following<i> employee&period;xml<&sol;i> file&period;<&sol;b><&sol;h2>&NewLine;<pre class&equals;"highlight">&lt&semi;&quest;xml version&equals;"1&period;0" encoding&equals;"UTF-8" standalone&equals;"yes"&quest;&gt&semi; &NewLine;&lt&semi;employee empid&equals;"1111"&gt&semi; &NewLine; &lt&semi;address&gt&semi; &NewLine; &lt&semi;city&gt&semi;Noida&lt&semi;&sol;city&gt&semi; &NewLine; &lt&semi;locality&gt&semi;Sector-88&lt&semi;&sol;locality&gt&semi; &NewLine; &lt&semi;pin&gt&semi;201301&lt&semi;&sol;pin&gt&semi; &NewLine; &lt&semi;&sol;address&gt&semi; &NewLine; &lt&semi;address&gt&semi; &NewLine; &lt&semi;city&gt&semi;Kannauj&lt&semi;&sol;city&gt&semi; &NewLine; &lt&semi;locality&gt&semi;Kamala Nagar&lt&semi;&sol;locality&gt&semi; &NewLine; &lt&semi;pin&gt&semi;209724&lt&semi;&sol;pin&gt&semi; &NewLine; &lt&semi;&sol;address&gt&semi; &NewLine; &lt&semi;name&gt&semi;Dinesh Rajput&lt&semi;&sol;name&gt&semi; &NewLine; &lt&semi;salary&gt&semi;100000&period;0&lt&semi;&sol;salary&gt&semi; &NewLine;&lt&semi;&sol;employee&gt&semi; &NewLine; &NewLine;<&sol;pre>&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;<div class&equals;"separator" style&equals;"clear&colon; both&semi; text-align&colon; center&semi;"><img src&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;wp-content&sol;uploads&sol;2014&sol;02&sol;111-jaxb-1&period;gif" border&equals;"0" &sol;><&sol;div>&NewLine;<h3><b>Download Source Code<&sol;b><br &sol;>&NewLine;<b><a href&equals;"https&colon;&sol;&sol;sites&period;google&period;com&sol;a&sol;dineshonjava&period;com&sol;dineshonjava&sol;dineshonjava&sol;JaxBMarshalExample&period;zip&quest;attredirects&equals;0&amp&semi;d&equals;1" target&equals;"&lowbar;blank" rel&equals;"noopener">JaxBMarshalExample&period;zip<&sol;a><&sol;b><&sol;h3>&NewLine;<&sol;div>&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;<&sol;div>&NewLine;<div style&equals;"background-color&colon; pink&semi; border-width&colon; thin&semi; text-align&colon; center&semi;"><b>&lt&semi;&lt&semi;<a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;jaxb-hello-world-example&sol;">Previous<&sol;a> &lt&semi;&lt&semi;   &vert;&vert; <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;jaxb-2x-tutorial&sol;">Index <&sol;a>&vert;&vert;   &gt&semi;&gt&semi;<a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;jaxb-unmarshalling-example&sol;" target&equals;"&lowbar;blank" rel&equals;"noopener">Next<&sol;a> &gt&semi;&gt&semi;<&sol;b><&sol;div>&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;<&sol;div>&NewLine;<div class&equals;"wp-post-navigation"> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <div class&equals;"wp-post-navigation-pre"> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;jaxb-hello-world-example&sol;">Previous<&sol;a> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <&sol;div> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <div class&equals;"wp-post-navigation-next"> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;jaxb-unmarshalling-example&sol;">Next<&sol;a> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab; <&sol;div> &NewLine;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;&Tab;<&sol;div>&NewLine;<script type&equals;"text&sol;javascript">&NewLine;jQuery&lpar;document&rpar;&period;ready&lpar;function&lpar;&dollar;&rpar; &lbrace;&NewLine; &dollar;&period;post&lpar;'https&colon;&sol;&sol;dineshonjava&period;com&sol;wp-admin&sol;admin-ajax&period;php'&comma; &lbrace;action&colon; 'mts&lowbar;view&lowbar;count'&comma; id&colon; '228'&rcub;&rpar;&semi;&NewLine;&rcub;&rpar;&semi;&NewLine;<&sol;script>

Dinesh Rajput

Dinesh Rajput is the chief editor of a website Dineshonjava, a technical blog dedicated to the Spring and Java technologies. It has a series of articles related to Java technologies. Dinesh has been a Spring enthusiast since 2008 and is a Pivotal Certified Spring Professional, an author of a book Spring 5 Design Pattern, and a blogger. He has more than 10 years of experience with different aspects of Spring and Java design and development. His core expertise lies in the latest version of Spring Framework, Spring Boot, Spring Security, creating REST APIs, Microservice Architecture, Reactive Pattern, Spring AOP, Design Patterns, Struts, Hibernate, Web Services, Spring Batch, Cassandra, MongoDB, and Web Application Design and Architecture. He is currently working as a technology manager at a leading product and web development company. He worked as a developer and tech lead at the Bennett, Coleman & Co. Ltd and was the first developer in his previous company, Paytm. Dinesh is passionate about the latest Java technologies and loves to write technical blogs related to it. He is a very active member of the Java and Spring community on different forums. When it comes to the Spring Framework and Java, Dinesh tops the list!

Share
Published by
Dinesh Rajput

Recent Posts

Strategy Design Patterns using Lambda

Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…

4 years ago

Decorator Pattern using Lambda

Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…

4 years ago

Delegating pattern using lambda

Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…

4 years ago

Spring Vs Django- Know The Difference Between The Two

Technology has emerged a lot in the last decade, and now we have artificial intelligence;…

4 years ago

TOP 20 MongoDB INTERVIEW QUESTIONS 2022

Managing a database is becoming increasingly complex now due to the vast amount of data…

4 years ago

Scheduler @Scheduled Annotation Spring Boot

Overview In this article, we will explore Spring Scheduler how we could use it by…

4 years ago