Wednesday, May 24, 2023

How to converts Java Object to XML - JAXB Example

 To convert a Java object to XML using JAXB (Java Architecture for XML Binding), you need to follow these steps:


Step 1: Set up your project

Make sure you have the necessary dependencies for JAXB in your project. JAXB is included in Java SE starting from Java SE 6, so you don't need to download any additional JAR files.


Step 2: Annotate your Java class

Annotate your Java class with JAXB annotations to specify how it should be marshalled (converted) to XML. These annotations provide instructions to JAXB for XML mapping.


Here's an example of a Java class with JAXB annotations:



import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name = "employee")
public class Employee {
    private String firstName;
    private String lastName;
    private int age;

    public Employee() {
    }

    public Employee(String firstName, String lastName, int age) {
        this.firstName = firstName;
        this.lastName = lastName;
        this.age = age;
    }

    @XmlElement
    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    @XmlElement
    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    @XmlElement
    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

In this example, the class Employee is annotated with @XmlRootElement to specify the root element name in the XML. The @XmlElement annotation is used for each property that should be included in the XML. 

Step 3: Convert the Java object to XML To convert an instance of your Java class to XML, you can use the javax.xml.bind.JAXB class. Here's an example of how to convert an Employee object to XML:


import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import java.io.StringWriter;

public class ObjectToXmlExample {
    public static void main(String[] args) {
        Employee employee = new Employee("John", "Doe", 30);

        try {
            JAXBContext jaxbContext = JAXBContext.newInstance(Employee.class);
            Marshaller marshaller = jaxbContext.createMarshaller();

            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

            StringWriter stringWriter = new StringWriter();
            marshaller.marshal(employee, stringWriter);

            String xml = stringWriter.toString();
            System.out.println(xml);
        } catch (JAXBException e) {
            e.printStackTrace();
        }
    }
}

In this example, we create an instance of Employee, and then we create a JAXBContext using the JAXBContext.newInstance() method, passing the Employee.class as the argument. Then we create a Marshaller from the JAXBContext and set the Marshaller.JAXB_FORMATTED_OUTPUT property to true to generate formatted XML. 

Next, we create a StringWriter to capture the XML output, and finally, we call the marshaller.marshal() method, passing the employee object and the StringWriter to convert the object to XML. The XML is then printed to the console. That's it! You have successfully converted a Java object to XML using JAXB.

No comments:

Post a Comment