The term JAXB stands for Java Architecture for XML Binding. Java programmers may use it to translate Java classes to XML representations. Java objects may be marshaled into XML and vice versa using JAXB. Sun provides an OXM (Object XML Mapping) or O/M framework.
Note: The biggest and only advantage of JAXB is as there’s no need to develop callback methods or create a SAX or DOM parser.
Implementation: Spring and JAXB Integration (Marshalling Java Object into XML)
The project Structure will look as follows:

We build Marshaller and Unmarshaller instances using the Jaxb2Marshaller instance. In Jaxb2Marshaller, set the jaxb.formatted.output attribute to true for nice printing. To map XML tags and attributes to Java objects, we need to construct a Java bean with properties that are mapped to XML tags and attributes. Using javax.xml.bind, annotate properties. XmlAttribute and XmlElement, for example, are annotations that link XML attributes and elements to Java bean properties.
Remember: To run the application, we need the following software as listed:
- Java 7
- Gradle
- Eclipse
- Spring-Oxm:4.1.5.RELEASE
Gradle File for Spring OXM and Spring Boot Starter
Step 1: Find the Gradle file for spring OXM and spring Boot Starter.
File: build.xml
XML
apply plugin: 'java'
apply plugin: 'eclipse'
archivesBaseName = 'geeksforgeeks'
version = '1'
repositories {
mavenCentral()
}
dependencies {
compile 'org.springframework.boot:spring-boot-starter:1.2.2.RELEASE'
compile 'org.springframework:spring-oxm:4.1.5.RELEASE'
}
|
Annotation Based Configuration File for Jaxb2Marshaller and Pretty Print
The Jaxb2Marshaller instance in Spring OXM is used to construct Marshaller and Unmarshaller instances.
Step 2: In Jaxb2Marshaller, we may configure settings for nice XML printing, encoding, and so on. To create a beautiful print, follow the instructions below.
map.put("jaxb.formatted.output", true);
jaxb2Marshaller.setMarshallerProperties(map);
File: AppConfig.java
Java
package com.geeksforgeeks;
import java.util.HashMap;
import java.util.Map;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.oxm.jaxb.Jaxb2Marshaller;
@Configuration
public class AppConfig {
@Bean public Processor getHandler()
{
Processor handler = new Processor();
handler.setMarshaller(getCastorMarshaller());
handler.setUnmarshaller(getCastorMarshaller());
return handler;
}
@Bean public Jaxb2Marshaller getCastorMarshaller()
{
Jaxb2Marshaller jaxb2Marshaller
= new Jaxb2Marshaller();
jaxb2Marshaller.setPackagesToScan(
"com.geeksforgeeks.bean" );
Map<String, Object> map
= new HashMap<String, Object>();
map.put( "jaxb.formatted.output" , true );
jaxb2Marshaller.setMarshallerProperties(map);
return jaxb2Marshaller;
}
}
|
Java supports many annotations for XML and Java object mapping, including @XmlRootElement, @XmlAccessorType, @XmlAttribute, and @XmlElement.
Step 3: Use @XmlAttribute to acquire java properties as XML attributes, and @XmlElement annotation on properties to retrieve XML subtags.
File: Company.java
Java
package com.geeksforgeeks.bean;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement (name = "company-info" ,
namespace = "com.geeksforgeeks" )
@XmlAccessorType (XmlAccessType.NONE)
public class Company {
@XmlAttribute (name = "id" ) private Integer id;
@XmlElement (name = "company-name" )
private String companyName;
@XmlElement (name = "ceo-name" ) private String ceoName;
@XmlElement (name = "no-emp" ) private Integer noEmp;
public Integer getId() { return id; }
public void setId(Integer id)
{
this .id = id;
}
public String getCompanyName() { return companyName; }
public void setCompanyName(String companyName)
{
this .companyName = companyName;
}
public String getCeoName() { return ceoName; }
public void setCeoName(String ceoName)
{
this .ceoName = ceoName;
}
public Integer getNoEmp() { return noEmp; }
public void setNoEmp(Integer noEmp)
{
this .noEmp = noEmp;
}
}
|
Define Method for Marshaller and Unmarshaller
Step 4: Find a utility method in which we are calling Marshaller and Unmarshaller.
File: Processor.java
Java
package com.geeksforgeeks;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import org.springframework.oxm.Marshaller;
import org.springframework.oxm.Unmarshaller;
public class Processor {
private Marshaller marshaller;
private Unmarshaller unmarshaller;
public void setMarshaller(Marshaller marshaller) {
this .marshaller = marshaller;
}
public void setUnmarshaller(Unmarshaller unmarshaller) {
this .unmarshaller = unmarshaller;
}
public void objectToXML(String fileName, Object graph) throws IOException {
FileOutputStream fos = null ;
try {
fos = new FileOutputStream(fileName);
marshaller.marshal(graph, new StreamResult(fos));
}
finally {
fos.close();
}
}
public Object xmlToObject(String fileName) throws IOException {
FileInputStream fis = null ;
try {
fis = new FileInputStream(fileName);
return unmarshaller.unmarshal( new StreamSource(fis));
}
finally {
fis.close();
}
}
}
|
Step 5: Run the application
Now in order to test the program, we can create a bean object, convert it to XML, and then transform that XML back to a Java object.
File: RunApplication.java
Java
package com.geeksforgeeks;
import com.geeksforgeeks.bean.Company;
import java.io.IOException;
import org.springframework.beans.BeansException;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class RunApplication {
public static void main(String[] args)
throws IOException
{
try (AnnotationConfigApplicationContext ctx
= new AnnotationConfigApplicationContext()) {
ctx.register(AppConfig. class );
ctx.refresh();
Processor processor
= ctx.getBean(Processor. class );
Company company = new Company();
company.setId( 1000 );
company.setCompanyName( "XYZ" );
company.setCeoName( "ABCD" );
company.setNoEmp( 100 );
processor.objectToXML( "country.xml" , company);
System.out.println( "Marshalling performed" );
company = (Company)processor.xmlToObject(
"country.xml" );
System.out.println(
"After UnMarshalling Data is: id:"
+ company.getId() + ", CountryName:"
+ company.getCompanyName());
}
catch (BeansException | IllegalStateException e) {
e.printStackTrace();
}
}
}
|
Output: java object to XML is as follows:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns2:company-info xmlns:ns2="com.geeksforgeeks" id="100">
<company-name>XYZ</company-name>
<ceo-name>ABCD</ceo-name>
<no-emp>100</no-emp>
</ns2:company-info>
Output: XML to java object is as follows:

Feeling lost in the vast world of Backend Development? It's time for a change! Join our
Java Backend Development - Live Course and embark on an exciting journey to master backend development efficiently and on schedule.
What We Offer:
- Comprehensive Course
- Expert Guidance for Efficient Learning
- Hands-on Experience with Real-world Projects
- Proven Track Record with 100,000+ Successful Geeks
Last Updated :
23 May, 2022
Like Article
Save Article