0
我試圖使用JAXB爲一個類創建幾個對象列表來加載一系列XML文件。JAXB Marshall/Unmarshall帶有List變量成員的類對象
瓷磚類
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;
}
}
所以,當我真的想解組XML,我想這是一個好主意,先整理一下類的測試實例,確認生成的XML格式文件。
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"));
生成的XML如下所示。
<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>
JAXB_List類在以下URL中詳細說明,它是我的另一個問題。 (沒有人顯然是有它的答案。 https://stackoverflow.com/questions/38472060/dynamic-xmlrootelement-name
所以生成的XML具有除了兩岸成員變量的所有值。
我在哪裏去了?
我還注意到,我沒有填充列表雙方! DOH! –
JamesA