编组不工作:在嵌套Dto的情况下

时间:2012-10-19 15:21:57

标签: java serialization xml-serialization xstream

我将我的移民详细信息保存在xml文件中,其结构有点像这样

<A>
    <b>
        <name>xxxxx</name>
        <age>xxxxx</age>
        <c>
          <someFeilds>yyyy</someFeilds>
        </c>
        <c>
          <someFeilds>yyyy</someFeilds>
        </c>
        <c>
          <someFeilds>yyyy</someFeilds>
        </c>
    </b>
    <b>
        <name>xxxxx</name>
        <age>xxxxx</age>
        <c>
            <someFeilds>yyyy</someFeilds>
        </c>
    </b>
</A>

我能够将其编组到我的DTO A谁的结构

class A {
    public ArrayList<B> bdtoInst;
}
class B {
    public String name;
    public String age;
    public ArrayList<C> cdtoInst;
}
class C {
    public String someFeilds;
}

Xstream声明是

    XStream xStream = new XStream();
    xStream.alias("A", A.class);
    xStream.addImplicitCollection(A.class, "bdtoInst");
    xStream.alias("b", B.class);
    xStream.addImplicitCollection(B.class, "cdtoInst");
    xStream.alias("c", C.class);

我可以把它整理到我的DTO,但当我解组它时,我没有得到正确的格式,它是这样的:

<A>
    <b>
        <name>xxxxx</name>
        <age>xxxxx</age>
        <c>
            <someFeilds>yyyy</someFeilds>
        </c>
        <c>
            <someFeilds>yyyy</someFeilds>
        </c>
        <c>
            <someFeilds>yyyy</someFeilds>
        </c>
        <c>
            <someFeilds>yyyy</someFeilds>
        </c>
    </b>
    <b>
        <name>xxxxx</name>
        <age>xxxxx</age>
        <c reference="../../b/c"/>
    </b>
</A>

我正在使用 xstream-1.4.3.jar。

1 个答案:

答案 0 :(得分:0)

因为xstream链接中使用了XPATH_RELATIVE_REFERENCES:xstream.codehaus.org/graphs.html 在xstream声明中我们必须添加

  xstream.setMode(XStream.NO_REFERENCES);

而不是标记参考值。