如何在不丢失源或目标的情况下从xml动态添加边

时间:2013-10-13 14:35:08

标签: mxgraph

我正在尝试从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);
}

1 个答案:

答案 0 :(得分:0)

我的问题是有多个mxCodecs ...我已经对代码进行了很多更改但这是基本问题