Categories: JAXB

JAXB Hello World 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;">&NewLine;<div dir&equals;"ltr" style&equals;"text-align&colon; justify&semi;">&NewLine;<p>JAXB&comma; stands for Java Architecture for XML Binding&comma; using JAXB annotation to convert Java object to &sol; from XML file&period; In this tutorial&comma; we show you how to use JAXB to do following stuffs &colon;<&sol;p>&NewLine;<ul style&equals;"text-align&colon; left&semi;">&NewLine;<li><b>Marshalling <&sol;b>– Convert a Java object into a XML file&period;<&sol;li>&NewLine;<li><b>Unmarshalling <&sol;b>– Convert XML content into a Java Object&period;<&sol;li>&NewLine;<&sol;ul>&NewLine;<h3><b>Technologies used in this article<&sol;b><&sol;h3>&NewLine;<ul style&equals;"text-align&colon; left&semi;">&NewLine;<li>JDK 1&period;6<&sol;li>&NewLine;<li>JAXB 2&period;2&period;7<&sol;li>&NewLine;<&sol;ul>&NewLine;<h2>JAXB Hello World Example<&sol;h2>&NewLine;<p>Working with JAXB is easy&comma; just annotate object with JAXB annotation&comma; later use <i><b>jaxbMarshaller&period;marshal&lpar;&rpar; <&sol;b><&sol;i>or <b><i>jaxbMarshaller&period;unmarshal&lpar;&rpar;<&sol;i><&sol;b> to do the object &sol; XML conversion&period;<&sol;p>&NewLine;<p><b>Simple JAXB Marshalling Example&colon; Converting Object into XML<&sol;b><&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;<p><b>Employee&period;java<&sol;b><&sol;p>&NewLine;<pre class&equals;"highlight">package com&period;doj&period;jaxb&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 String 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 String getAddress&lpar;&rpar; &lbrace; &NewLine; return address&semi; &NewLine; &rcub; &NewLine; public void setAddress&lpar;String address&rpar; &lbrace; &NewLine; this&period;address &equals; address&semi; &NewLine; &rcub; &NewLine; &NewLine; &NewLine;&rcub; &NewLine;<&sol;pre>&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;<ul style&equals;"text-align&colon; left&semi;">&NewLine;<li><b>&commat;XmlRootElement <&sol;b>specifies the root element for the xml document&period;<&sol;li>&NewLine;<li><b>&commat;XmlAttribute <&sol;b>specifies the attribute for the root element&period;<&sol;li>&NewLine;<li><b>&commat;XmlElement<&sol;b> specifies the sub element for the root element&period;<&sol;li>&NewLine;<&sol;ul>&NewLine;<p>&nbsp&semi;<&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; &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 ObjectToXMLTest &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; 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;"Noida"&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;<p>Run this application as java program lets see following output file in application<br &sol;>&NewLine;<b>employee&period;xml<&sol;b><&sol;p>&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;Noida&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;<&sol;div>&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-2&period;gif" border&equals;"0" &sol;><&sol;div>&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;<&sol;div>&NewLine;<h3><b>Download Source Code with libs<&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;JaxBExample&period;zip&quest;attredirects&equals;0&amp&semi;d&equals;1" target&equals;"&lowbar;blank" rel&equals;"noopener">JaxBExample&period;zip<&sol;a><&sol;b><&sol;h3>&NewLine;<&sol;div>&NewLine;<p>&nbsp&semi;<&sol;p>&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-2x-tutorial&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-marshalling-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-2x-tutorial&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-marshalling-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; '229'&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