有没有办法设置Xstream序列化的顺序

时间:2013-02-28 18:11:26

标签: java android xstream

我需要从Android上的java对象生成XML。 XML节点必须按照确定的顺序。 XML节点的到期XStream documentation顺序匹配对象的字段定义。当我使用java类(String,Date ...)作为字段时没有问题。但是当我需要将对象序列化为字段时会出现问题。

这是我的代码:

final XStream x = new XStream();

x.autodetectAnnotations(true);

SecondEntity secondEntity = new SecondEntity();
secondEntity.setSecondaryDate(new Date());
secondEntity.setSecondaryString("Secondary String");

InnerEntity innerEntity = new InnerEntity();
innerEntity.setInnerDate(new Date());
innerEntity.setInnerString("Inner String");

SomeEntity someEntity = new SomeEntity();
someEntity.setInnerEntity(innerEntity);
someEntity.setSecondEntity(secondEntity);
someEntity.setSomeDate(new Date());
someEntity.setSomeString("Some string");

x.toXML(someEntity)

SomeEntity:

@XStreamAlias("SomeEntity")
public class SomeEntity {
    @XStreamAlias("innerEntity")
    private InnerEntity innerEntity;
    @XStreamAlias("secondEntity")
    private SecondEntity secondEntity;
    @XStreamAlias("someString")
    private String someString;
    @XStreamAlias("someDate")
    private Date someDate;

    public InnerEntity getInnerEntity() {
        return innerEntity;
    }

    public void setInnerEntity(InnerEntity innerEntity) {
        this.innerEntity = innerEntity;
    }

    public SecondEntity getSecondEntity() {
        return secondEntity;
    }

    public void setSecondEntity(SecondEntity secondEntity) {
        this.secondEntity = secondEntity;
    }

    public String getSomeString() {
        return someString;
    }

    public void setSomeString(String someString) {
        this.someString = someString;
    }

    public Date getSomeDate() {
        return someDate;
    }

    public void setSomeDate(Date someDate) {
        this.someDate = someDate;
    }
}

InnerEntity:

@XStreamAlias("InnerEntity")
public class InnerEntity {
    @XStreamAlias("innerString")
    private String innerString;
    @XStreamAlias("innerDate")
    private Date innerDate;

    public String getInnerString() {
        return innerString;
    }

    public void setInnerString(String innerString) {
        this.innerString = innerString;
    }

    public Date getInnerDate() {
        return innerDate;
    }

    public void setInnerDate(Date innerDate) {
        this.innerDate = innerDate;
    }
}

SecondEntity:

@XStreamAlias("SecondEntity")
public class SecondEntity {
    @XStreamAlias("secondaryString")
    private String secondaryString;
    @XStreamAlias("secondaryDate")
    private Date secondaryDate;

    public String getSecondaryString() {
        return secondaryString;
    }

    public void setSecondaryString(String secondaryString) {
        this.secondaryString = secondaryString;
    }

    public Date getSecondaryDate() {
        return secondaryDate;
    }

    public void setSecondaryDate(Date secondaryDate) {
        this.secondaryDate = secondaryDate;
    } 
}

我得到了

<SomeEntity>
    <innerEntity>
        <innerDate>2013-02-28 18:04:24.184 UTC</innerDate>
        <innerString>Inner String</innerString>
    </innerEntity>
    <secondEntity>
        <secondaryDate>2013-02-28 18:04:24.183 UTC</secondaryDate>
        <secondaryString>Secondary String</secondaryString>
    </secondEntity>
    <someDate>2013-02-28 18:04:24.184 UTC</someDate>
    <someString>Some string</someString>
</SomeEntity>

当我需要时:

<SomeEntity>
    <innerEntity>
        <innerString>Inner String</innerString>
        <innerDate>2013-02-28 18:04:24.184 UTC</innerDate>
    </innerEntity>
    <secondEntity>
        <secondaryString>Secondary String</secondaryString>
        <secondaryDate>2013-02-28 18:04:24.183 UTC</secondaryDate>
    </secondEntity>
    <someDate>2013-02-28 18:04:24.184 UTC</someDate>
    <someString>Some string</someString>
</SomeEntity>

1 个答案:

答案 0 :(得分:0)

请为InnerEntity和SecondEntity实施此界面FieldKeySorter