deserializing many child nodes to add references to objects xStream java

自闭症网瘾萝莉.ら 提交于 2019-12-25 01:56:11

问题


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.


回答1:


Read about Object references in XStream.

xstream.setMode(XStream.ID_REFERENCES);


来源:https://stackoverflow.com/questions/13142025/deserializing-many-child-nodes-to-add-references-to-objects-xstream-java

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!