JamesA JamesA - 1 year ago 119
Java Question

JAXB Marshall/Unmarshall a class object with List variable member

I'm trying to load a series of XML files using JAXB to create several Lists of Objects for each Class.

Tile Class

import java.util.List;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;

public class Tile {

private String name;
public String getName(){
return name;
public void setName(String arg){
this.name = arg;

private int id;
public int getId() {
return id;
public void setId(int arg){
this.id = arg;

private String imageName;
public String getImageName(){
return imageName;
public void setImageName(String imageName) {
this.imageName = imageName;

private int drawType;
public int getDrawType(){
return drawType;
public void setDrawType(int drawType) {
this.drawType = drawType;

private int trueType;
public int getTrueType() {
return trueType;
public void setTrueType(int trueType) {
this.trueType = trueType;

private int count;
public int getCount(){
return count;
public void setCount(int count) {
this.count = count;

private List<Integer> side;
public Integer getSide(int arg){
return side.get(arg);
public void setSide(List<Integer> side) {
this.side = side;

private int feature;
public int getFeature (){
return feature;
public void setFeature(int feature) {
this.feature = feature;

So while I'm really trying to unmarshal the XML, I figured it is a good idea to first Marshal a test instance of the class, to confirm the format of the resulting XML file.

JAXBContext jc = JAXBContext.newInstance(Tile.class,JAXB2_Lists.class,Feature.class );

JAXB2_Lists<Tile> exportTest = new JAXB2_Lists<>();
Marshaller marshaller = jc.createMarshaller();

Tile testTile = new Tile();
testTile.setName("Test Name");
List<Integer> sides = new ArrayList<>();


marshaller.marshal(exportTest, new File("TilesExport.xml"));

The resulting XML looks like the following.

<name>Test Name</name>

The JAXB_List class is detailed at the following URL, another question of mine. (no one apparently has an answer for it.
Dynamic XmlRootElement name?

So the resulting XML has all the values except for the Sides member variable.

Where am I going wrong?

Answer Source

You need a public getter for your sides list, and the setter needs to be named sides. Something like:

public List<Integer> getSides() {
    return side;
public void setSides(List<Integer> sides) {
    this.side = sides;

should do it. Might also be worth renaming your side field to sides for the sake of consistency.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download