【发布时间】:2015-03-31 18:44:55
【问题描述】:
我快到了,但只是找不到让我的 ArrayList 的内容正确序列化的方法。除了在 XML 文件中为列表中的每个对象生成条目的 List 之外,一切都运行良好,但实际上并没有存储任何我可以用来重新创建类文件的数据。
当前的输出是这样的(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();
}
感谢任何帮助。谢谢
【问题讨论】:
标签: xml json serialization jaxb