当我通过将第一个节点、剩余节点和边存储在 xml 文件中来将图形从 java 序列化为 xml 时,它会被存储。但是当我打开 xml 文件时,它给了我一个错误“XML 文档中只允许一个顶级元素错误处理资源'file:///D:/DM1/tro.xml'。第 17 行,位置...
</GraphPanel_-Node><GraphPanel_-Node>
--------------------^
这是代码
private class Serialize extends AbstractAction {
XStream xs = new XStream();
public Serialize(String name) {
super(name);
}
public void actionPerformed(ActionEvent e) {
ListIterator<Node> iter = nodes.listIterator();
while (iter.hasNext()) {
Node n = iter.next();
try {
FileOutputStream fs = new FileOutputStream("D:/DM1/tro.xml",true);
if(!n.fnode){
xs.toXML(n, fs);
}
} catch (FileNotFoundException ex) {
ex.printStackTrace();
}
iter.remove();
}
ListIterator<Edge> Eiter = edges.listIterator();
while (Eiter.hasNext()) {
Edge edg = Eiter.next();
try {
FileOutputStream fs = new FileOutputStream("D:/DM1/tro.xml",true);
xs.toXML(edg, fs);
} catch (FileNotFoundException ex) {
ex.printStackTrace();
}
Eiter.remove();
}
ListIterator<Node> Fiter = nodes.listIterator();
while (Fiter.hasNext()) {
Node n = Fiter.next();
try {
FileOutputStream fs = new FileOutputStream("D:/DM1/tro.xml",true);
if(n.fnode){
FirstNode f =new FirstNode("Root");
xs.toXML(f, fs);
}
} catch (FileNotFoundException ex) {
ex.printStackTrace();
}
Fiter.remove();
}
}
}