Marshalling is the process of converting a Java object into XML format, making it easier to store or share data in a platform-independent manner. JAXB (Java Architecture for XML Binding) is a popular framework in Java for handling XML and binding it to Java objects. It simplifies the task of mapping Java classes to XML representations and vice versa.
In this post, we'll walk through the process of converting Java objects to XML using JAXB.
What Is JAXB?
JAXB is a part of the Java Standard Edition and provides a standard way to map Java objects to XML and back. It uses annotations to define the relationship between Java classes and XML elements, making it easy to serialize objects into XML or deserialize XML into objects.
Key Steps in Marshalling Using JAXB
Here's a step-by-step guide to converting a Java object into XML:
1. Add JAXB Dependency
If you are using Java 9 or above, JAXB is no longer included in the JDK. Add the following
Maven dependency to your pom.xml
file:
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.1</version>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-impl</artifactId>
<version>2.3.1</version>
</dependency>
2. Create a Java Class
Define the Java class that represents the data you want to convert to XML. Use JAXB annotations to specify how the class maps to XML.
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement // Marks this class as the root element of the XML
public class Employee {
private int id;
private String name;
private double salary;
// Default constructor is required for JAXB
public Employee() {}
public Employee(int id, String name, double salary) {
this.id = id;
this.name = name;
this.salary = salary;
}
@XmlElement // Maps this field to an XML element
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@XmlElement
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@XmlElement
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
}
3. Perform Marshalling
Write the logic to convert the Employee
object into an XML string or file.
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import java.io.File;
public class MarshallingExample {
public static void main(String[] args) {
try {
// Create an Employee object
Employee employee = new Employee(101, "John Doe", 75000.0);
// Create a JAXB context for the Employee class
JAXBContext context = JAXBContext.newInstance(Employee.class);
// Create a Marshaller
Marshaller marshaller = context.createMarshaller();
// Format the XML output for better readability
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
// Marshal the object to a file
marshaller.marshal(employee, new File("employee.xml"));
// Marshal the object to the console
marshaller.marshal(employee, System.out);
} catch (JAXBException e) {
e.printStackTrace();
}
}
}
4. Output XML
Running the above program will generate an XML file (employee.xml
) and print the following output to the console:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<employee>
<id>101</id>
<name>John Doe</name>
<salary>75000.0</salary>
</employee>
JAXB Annotations Cheat Sheet
Here are some commonly used JAXB annotations:
Annotation | Purpose |
---|
@XmlRootElement | Specifies the root element in the XML document. |
@XmlElement | Maps a field to an XML element. |
@XmlAttribute | Maps a field to an XML attribute. |
@XmlType | Defines the order of elements in the XML schema. |
@XmlTransient | Excludes a field from the XML output. |
Benefits of Using JAXB
- Ease of Use: JAXB simplifies XML handling with minimal configuration.
- Annotation-Based: The annotations are straightforward, reducing the need for external mapping files.
- Integration: JAXB works seamlessly with Java and is widely supported in various frameworks.