JamesA JamesA - 4 months ago 14
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;

@XmlRootElement//(name="Tiles")
@XmlAccessorType(XmlAccessType.FIELD)
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.setCount(1);
testTile.setDrawType(1);
testTile.setFeature(1);
testTile.setId(1);
testTile.setImageName("TestImage.png");
testTile.setName("Test Name");
testTile.setTrueType(1);
List<Integer> sides = new ArrayList<>();
testTile.setSide(sides);

exportTest.getValues().add(testTile);

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


The resulting XML looks like the following.

<jaxb2Lists>
<tile>
<name>Test Name</name>
<id>1</id>
<imageName>TestImage.png</imageName>
<drawType>1</drawType>
<trueType>1</trueType>
<count>1</count>
<feature>1</feature>
</tile>
</jaxb2Lists>


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

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.