无法使用 JAXB 将 class 序列化为 XML
Trouble serializing a class into XML using JAXB
我快完成了,但就是找不到一种方法来正确序列化我的 ArrayList 的内容。除了为列表中的每个对象在 XML 文件中生成条目的列表外,一切都运行良好,但实际上并没有存储任何我可以用来重新创建 class 文件的数据。
当前输出是这样的(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();
}
感谢任何帮助。谢谢
@XmlTransient
防止数据被转换 to/from XML。这就是该注释的目的,您在整个模型中都有它。
我快完成了,但就是找不到一种方法来正确序列化我的 ArrayList 的内容。除了为列表中的每个对象在 XML 文件中生成条目的列表外,一切都运行良好,但实际上并没有存储任何我可以用来重新创建 class 文件的数据。
当前输出是这样的(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();
}
感谢任何帮助。谢谢
@XmlTransient
防止数据被转换 to/from XML。这就是该注释的目的,您在整个模型中都有它。