How to generate a wrapper element around XML representation in JAXB?

In this example you’ll learn how to use the JAXB @XmlElementWrapper annotation. This annotation can be use to generate a wrapper element around an XML element representation. When no name defined, the @XmlElementWrapper annotation uses the property name as the wrapper element name. Let’s see the code snippet below.

package org.kodejava.example.jaxb;

import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;
import java.util.ArrayList;
import java.util.List;

public class Record {
    private Integer id;
    private String title;
    private List tracks = new ArrayList<>();

    public Integer getId() {
        return id;

    public void setId(Integer id) { = id;

    public String getTitle() {
        return title;

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

    @XmlElementWrapper(name = "trackList")
    @XmlElement(name = "track")
    public List getTracks() {
        return tracks;

    public void setTracks(List tracks) {
        this.tracks = tracks;

Below is the code snippet for marshaling the Record object into an XML document. Before the marshalling take place we create an object of Record and adds a some Tracks into it.

package org.kodejava.example.jaxb;

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

public class JAXBElementWrapper {
    public static void main(String[] args) {
        Track track1 = new Track();
        track1.setTitle("Love Me Do");

        Track track2 = new Track();
        track2.setTitle("From Me To You");

        Track track3 = new Track();
        track3.setTitle("She Loves You");

        Record record = new Record();
        record.setTitle("The Beatles 1");

        try {
            JAXBContext context = JAXBContext.newInstance(Record.class);
            Marshaller marshaller = context.createMarshaller();
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            marshaller.marshal(record, System.out);

            Writer writer = null;
            try {
                writer = new FileWriter("Record.xml");
                marshaller.marshal(record, writer);
            } finally {
                if (writer != null) {
        } catch (JAXBException e) {
        } catch (IOException e) {

The above program will generated the following XML document.

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<record id="1">
    <title>The Beatles 1</title>
        <track id="1">
            <title>Love Me Do</title>
        <track id="2">
            <title>From Me To You</title>
        <track id="3">
            <title>She Loves You</title>
Wayan Saryada

Wayan Saryada

A programmer, runner, recreational diver, currently living in the island of Bali, Indonesia. Mostly programming in Java, creating web based application with Spring Framework, JPA, etc. If you need help on Java programming you can hire me on Fiverr.
Wayan Saryada


Leave a Reply