反序列化许多子节点以添加对对象的引用 xStream java [英] 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>
我需要将子节点与图列表中其他节点的引用链接,这意味着在解组过程中,我不能只创建一个新节点并使用给我读者的属性设置它的 id,我需要它与图上已经存在的节点共享它的属性,以便能够做到这一点,我在 GraphConverter 类中尝试使用以下函数:
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.
推荐答案
阅读对象引用在 XStream 中.
Read about Object references in XStream.
xstream.setMode(XStream.ID_REFERENCES);
这篇关于反序列化许多子节点以添加对对象的引用 xStream java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!