deserializing many child nodes to add references to objects xStream java

Hi I'm de serializing the following graph:

<grafo>
 <nodo id="1">
   <child id="2"/>
 </nodo>
 <nodo id="2">
   <child id="3"/>
   <child id="4"/>
 </nodo>
 <nodo id="3">
   <child id="4"/>
 </nodo>
 <nodo id="4">
   <child id="1"/>
 </nodo>

I need to link the child nodes with references of other nodes in the graph List, that means that in the unmarshalling process I can't just create a new Node and set it's id with the atribute that gives me the reader, I need it to share it's atributes with the nodes that are already on the graph, to be able to do that I was trying with the following function in a GraphConverter class:

public Object unmarshal(HierarchicalStreamReader reader,UnmarshallingContext arg1) {
    Grafo graph= new Grafo();
    ArrayList<ArrayList<String>> handlechilds= new ArrayList<ArrayList<String>>();
     while (reader.hasMoreChildren()) 
     {
         reader.moveDown();
         Nodo node= new Nodo(reader.getAttribute("id"));
         graph.nodos.add(node);
         reader.moveDown();
         //reader.getAttribute("id") ->> this just gives me the value of the fisrt node but not the anothers!!
         reader.moveUp();
         reader.moveUp();
     }
    return graph;
}

I was thinking to save the values of the edges and add the references in another for that iterates over the graph, but I realized that when the reader just returns one of the childs, and I need all of them.

Upvotes: 0

Views: 871

Answers (1)

artplastika
artplastika

Reputation: 1982

Read about Object references in XStream.

xstream.setMode(XStream.ID_REFERENCES);

Upvotes: 2

Related Questions