我正在尝试从 xml 文件构建图形,但仅加载基于特定条件的节点......我可以使用包含解码和添加功能的 for 循环添加所有顶点,
问题出在边缘..我可以让他们有来源,但目标总是空的
在我尝试的许多事情中,最终版本将 xml 节点添加到数组中,并且只有在所有顶点都添加到图中之后,我才对边进行解码......但这仍然不起作用......
function get_selected(main_class, filename){
"use strict";
var node, i, j, req, root, decj, deci, enc, dec, data, parent, graph, main_class_node, child_nodes, edges, destinations;
deci = new mxCodec();
decj = new mxCodec();
graph = new mxGraph();
parent = graph.getDefaultParent();
req = mxUtils.load(filename);
root = req.getDocumentElement();
child_nodes = [];
edges = [];
destinations = [];
mxLog.show();
for (i=0; i < root.childNodes[0].childNodes.length; i ++) {
// Get the main cell and add it to the graph
if (root.childNodes[0].childNodes[i].id == main_class) {
node = deci.decode(root.childNodes[0].childNodes[i]);
main_class_node = graph.getModel().add(parent, node);
}
//get all the children of the main cell and add them to the graph
else if (root.childNodes[0].childNodes[i].outerHTML.indexOf('parent="' + main_class + '"') != -1 ) {
node = decj.decode(root.childNodes[0].childNodes[i]);
graph.getModel().add(main_class_node, node);
child_nodes.push(node.id);
}
}
for (j=0; j < child_nodes.length; j ++) {
for (i=0; i < root.childNodes[0].childNodes.length; i ++) {
//get all the edges originating in any of the "chidren" and add them to the graph
if (root.childNodes[0].childNodes[i].outerHTML.indexOf('source="' + child_nodes[j] + '"') != -1 ) {
node = root.childNodes[0].childNodes[i];
//graph.getModel().add(parent, node);
edges.push(node);
}
}
}
for (j=0; j < edges.length; j ++) {
for (i=0; i < root.childNodes[0].childNodes.length; i ++) {
//get the target of each of the edges and and add them to the graph
if (root.childNodes[0].childNodes[i].id == "attr-" + edges[j].id.split('-')[1]) {
node = deci.decode(root.childNodes[0].childNodes[i]);
graph.getModel().add(parent, node);
}
}
}
dec = new mxCodec();
for (j=0; j < edges.length; j ++) {
node = dec.decode(edges[j]);
graph.getModel().add(parent, node);
}
enc = new mxCodec();
data = enc.encode(graph.getModel());
return mxUtils.getXml(data);
}