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;

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

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

    public void setId(Integer id) {
        this.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;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;

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

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

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

        Record record = new Record();
        record.setId(1);
        record.setTitle("The Beatles 1");
        record.getTracks().add(track1);
        record.getTracks().add(track2);
        record.getTracks().add(track3);

        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) {
                    writer.close();
                }
            }
        } catch (JAXBException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

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>
    <trackList>
        <track id="1">
            <title>Love Me Do</title>
        </track>
        <track id="2">
            <title>From Me To You</title>
        </track>
        <track id="3">
            <title>She Loves You</title>
        </track>
    </trackList>
</record>
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

2 Comments

Leave a Reply