0
我想從xml文件構建圖形,但只加載基於特定條件的節點...我可以獲取所有要添加的頂點對於包括解碼,並添加功能循環,如何從XML動態添加邊緣而不丟失源或目標
問題是邊緣..我可以讓他們有來源,但目標始終之間的很多事情我想最終版本拉閘空
添加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);
}