How to map a bean property to an XML attribute in JAXB?

In this example you will learn how to define a bean’s / pojo’s properties as an XML attribute in JAXB. To define properties as an XML attribute we use the @XmlAttribute annotation. In the Student class below the id property will be defined as an attribute of the student root element. While the name and grade property will be an element of the student.

package org.kodejava.xml.support;

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

@XmlRootElement
public class Student {
    private Integer id;
    private String name;
    private Integer grade;

    @XmlAttribute
    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getGrade() {
        return grade;
    }

    public void setGrade(Integer grade) {
        this.grade = grade;
    }
}

Here the program that we can use to convert the Student bean into an XML document.

package org.kodejava.xml;

import org.kodejava.xml.support.Student;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import java.io.FileWriter;
import java.io.IOException;

public class JAXBElementAttribute {
    public static void main(String[] args) {
        Student student = new Student();
        student.setId(1);
        student.setName("Alice");
        student.setGrade(12);

        try {
            JAXBContext context = JAXBContext.newInstance(Student.class);
            Marshaller marshaller = context.createMarshaller();
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            marshaller.marshal(student, new FileWriter("Student.xml"));
        } catch (JAXBException | IOException e) {
            e.printStackTrace();
        }
    }
}

When you run the program a file named Student.xml will be created with the following content:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<student id="1">
    <grade>12</grade>
    <name>Alice</name>
</student>

Maven Dependencies

<dependencies>
    <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-ri</artifactId>
        <version>2.3.5</version>
        <type>pom</type>
    </dependency>
</dependencies>

Maven Central Maven Central

How to convert object to XML using JAXB?

JAXB, Java Architecture for XML Binding, it uses JAXB annotations to convert POJO to or from XML file. In this example you will learn how to convert an object / POJO into an XML document using JAXB. The process of converting an object into XML also known as marshalling. In this snippet our POJO is a class called Track as you can see below.

The first thing that we need to do is to annotate the Track POJO with a couple of JAXB annotations. For this example we add the @XmlRootElement, @XmlElement and @XmlAttribute annotations.

package org.kodejava.xml.support;

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

@XmlRootElement
public class Track {
    private Integer id;
    private String title;

    public Track() {
    }

    public Integer getId() {
        return id;
    }

    @XmlAttribute
    public void setId(Integer id) {
        this.id = id;
    }

    @XmlElement
    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    @Override
    public String toString() {
        return "Track{" +
                "id=" + id +
                ", title='" + title + '\'' +
                '}';
    }
}

After we have the Track class annotated we can use the code snippet below to convert the POJO into XML. The step to take is to start by creating an instance of JAXBContext. From this context object we create the Marshaller object. To convert it to XML we called the marshall() method. The method have many overloads, in this case we just want to print the XML to the System.out.

package org.kodejava.xml;

import org.kodejava.xml.support.Track;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;

public class JAXBObjectToXml {
    public static void main(String[] args) {
        Track track = new Track();
        track.setId(1);
        track.setTitle("Hey Jude");

        try {
            JAXBContext context = JAXBContext.newInstance(Track.class);

            Marshaller marshaller = context.createMarshaller();
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            marshaller.marshal(track, System.out);
        } catch (JAXBException e) {
            e.printStackTrace();
        }
    }
}

If you run this code you will get the following output:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<track id="1">
    <title>Hey Jude</title>
</track>

Maven Dependencies

<dependencies>
    <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-ri</artifactId>
        <version>2.3.5</version>
        <type>pom</type>
    </dependency>
</dependencies>

Maven Central Maven Central