Search code examples
javaserializationxml-serializationxstream

Marshalling not working: In case of nested Dto


I am saving my configration details in an xml file its structure is somewat like this

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

I am able to marshal it to my DTO A whoes structure is

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

Xstream declartion is

    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);

I am able to marshal it to my DTO, but when i unmarshal it, i am not getting the correct format, it is coming like this:

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

I am using xstream-1.4.3.jar.


Solution

  • because of XPATH_RELATIVE_REFERENCES used in the xstream link:xstream.codehaus.org/graphs.html in the xstream declaration we have to add

      xstream.setMode(XStream.NO_REFERENCES);
    

    than instead of marking the refrence value will be placed.