【问题标题】:Trouble serializing a class into XML using JAXB使用 JAXB 将类序列化为 XML 时遇到问题
【发布时间】: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


    【解决方案1】:

    @XmlTransient 阻止数据与 XML 之间的转换。这就是该注释的目的,您的模型中到处都有它。

    【讨论】:

    • 我认为它们是必要的,因为当它们被删除时,我得到一个“类有两个同名的属性”错误。我之前曾与 XStream 一起工作,但我不允许添加额外的依赖项,只是找不到这样做的方法。感谢回复
    • @AidanO - 查看以下“两个属性”问题:blog.bdoughan.com/2011/06/using-jaxbs-xmlaccessortype-to.html
    • 谢谢,根据您的建议,我更改了已解决错误的访问权限,但列表仍未按我的意愿进行序列化,我已编辑主帖子以显示该元素的问题。
    • @AidanO - Ehat 坐标类看起来像。如果是您的班级,您将需要查看 @XmlValue 注释。如果不是,那么您可能需要写一个XmlAdapter
    • 看来我需要写一个 XmlAdapter,谢谢你的帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-25
    • 1970-01-01
    • 1970-01-01
    • 2019-03-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多