1

我需要从 Android 上的 java 对象生成 XML。XML 节点必须有明确的顺序。由于XML 节点的XStream 文档顺序匹配对象的字段定义。当我使用 java classes(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)

某实体:

@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;
    }
}

内部实体:

@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;
    }
}

第二实体:

@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>
4

1 回答 1

0

请为 InnerEntity 和 SecondEntity 实现此接口FieldKeySorter

于 2013-06-28T09:10:19.167 回答