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<Track> tracks = new ArrayList<>();
@XmlRootElement
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<Track> getTracks() {
return tracks;
}
public void setTracks(List<Track> 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 Track
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>
Latest posts by Wayan (see all)
- How do I install Calibri font in Ubuntu? - January 24, 2021
- How do I create a generic class in Java? - January 1, 2021
- How do I convert java.util.TimeZone to java.time.ZoneId? - April 25, 2020
Hi,
What is the structure of the Track class defined in the above example
Hi, sorry to confused you. Take a look at the following example: https://kodejava.org/how-to-convert-object-to-xml-using-jaxb/