我几乎就在那里但是找不到让我的ArrayList内容正确序列化的方法。除了列表中的每个对象在XML文件中生成条目之外,一切都运行良好,但实际上并不存储我可以用来重新创建类文件的任何数据。
当前输出是这个(fightAreaCoords是麻烦的元素):
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<userProfile>
<settings>
<targetSelection>1</targetSelection>
<useFood>false</useFood>
<exitOutFood>false</exitOutFood>
<lootInCombat>false</lootInCombat>
<useAbilities>true</useAbilities>
<useSoulsplit>false</useSoulsplit>
<waitForLoot>false</waitForLoot>
<looting>false</looting>
<buryBones>false</buryBones>
<quickPray>false</quickPray>
<exitOnPrayerOut>false</exitOnPrayerOut>
<tagMode>false</tagMode>
<tagSelection>0</tagSelection>
<foodAmount>0</foodAmount>
<fightRadius>20</fightRadius>
<eatValue>0</eatValue>
<prayValue>0</prayValue>
<criticalHitpoints>1000</criticalHitpoints>
</settings>
<fightAreaCoords>
<fightAreaCoords/>
<fightAreaCoords/>
<fightAreaCoords/>
<fightAreaCoords/>
<fightAreaCoords/>
<fightAreaCoords/>
</fightAreaCoords>
<npcNames>Cow</npcNames>
<profileName>test123</profileName>
</userProfile>
但我希望元素像这样序列化:
<fightAreaCoords>
<fightAreaCoords>Coordinate [3255, 3287, 0]</fightAreaCoords>
<fightAreaCoords>Coordinate [3242, 3231, 0]</fightAreaCoords>
</fightAreaCoords>
这是我尝试序列化的模型:
@XmlRootElement
@XmlAccessorType(XmlAccessType.PROPERTY)
public class UserProfile {
public String profileName;
public String[] npcNames;
public String[] lootNames;
public List<Coordinate> fightAreaCoords;
public List<Coordinate> bankAreaCoords;
public Area getBankArea() {
return new Area.Polygonal(bankAreaCoords.toArray(new Coordinate[bankAreaCoords.size()]));
}
public Area getFightArea() {
return new Area.Polygonal(fightAreaCoords.toArray(new Coordinate[fightAreaCoords.size()]));
}
@XmlElement
public Settings settings;
public void setProfileName(String name) {
profileName = name;
}
@XmlElement
public String getProfileName() {
return profileName;
}
public void setNpcNames(String[] names) {
npcNames = names;
}
@XmlElement
public String[] getNpcNames() {
return npcNames;
}
public void setLootNames(String[] names) {
lootNames = names;
}
@XmlElementWrapper
@XmlElement
public List<String> getLootNames() {
return Arrays.asList(lootNames);
}
public void setFightAreaCoords(List<Coordinate> coords) {
fightAreaCoords = coords;
}
@XmlElementWrapper
@XmlElement
public List<Coordinate> getFightAreaCoords() {
return fightAreaCoords;
}
public void setBankAreaCoords(List<Coordinate> coords) {
bankAreaCoords = coords;
}
@XmlElementWrapper
@XmlElement
public List<Coordinate> getBankAreaCoords() {
return bankAreaCoords;
}
}
我创建的是:
try {
File file = new File(Environment.getStorageDirectory().getAbsolutePath() + "/" + profile.toString() + ".xml");
JAXBContext context = JAXBContext.newInstance(UserProfile.class);
Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(profile, file);
// print out for easy debugging
marshaller.marshal(profile, System.out);
return true;
} catch (JAXBException e) {
e.printStackTrace();
}
感谢任何帮助。感谢
答案 0 :(得分:1)
@XmlTransient
可防止数据转换为XML或从XML转换。这就是注释的目的,你可以在模型中使用它。