Reputation: 47
I'm almost there but just cannot find a way to get the contents of my ArrayList to serialize correctly. Everything is working well apart from the List which produces entries in the XML file for each object in the list, but doesn't actually store any data that I could use to recreate the class file.
The current output is this (fightAreaCoords is the troublesome element):
<?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>
But I would like the element to serialize like this:
<fightAreaCoords>
<fightAreaCoords>Coordinate [3255, 3287, 0]</fightAreaCoords>
<fightAreaCoords>Coordinate [3242, 3231, 0]</fightAreaCoords>
</fightAreaCoords>
Here's the model I'm trying to serialize:
@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;
}
}
Which I create with:
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();
}
Any help is appreciated. thanks
Upvotes: 1
Views: 31
Reputation: 148977
@XmlTransient
prevents the data from being converted to/from XML. That's the purpose of that annotation and you have it all over your model.
Upvotes: 1