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