0

With the help of How to get the XML from mxGrpah diagram? able to get the dynamic XML from the graph editor. Now, I need to display the diagram by loading a XML (reverse algorithm)

Bala
  • 618
  • 5
  • 21

2 Answers2

4

Many thanks @NickAth, Also, I tried my own analysis found that the following solution is working fine.

var xml = {xml};
var ui = new EditorUi(new Editor());
var doc = mxUtils.parseXml(xml);
ui.editor.setGraphXml(doc.documentElement);
Bala
  • 618
  • 5
  • 21
  • 1
    Great! Just have in mind that this way you use another EditorUI different from the example's, now you also have access to the graph object using `ui.editor.graph` :) – NickAth Jul 17 '19 at 13:20
3

Ok, similarly to How to get the XML from mxGrpah diagram? answer, it is not again clear to me how to get the graph object in which the whole graph is saved so I propose the following workaround:

1) Get the reference of the original graph object into your index.html code

i. Add a global variable into index.html to get the reference of the graph object

var universalGraph;

ii. Go into a .js file where the graph object has already been created (for example go to js/view/mxGraphView on installListeners function and assign the instance of the graph object to your global accessible reference

mxGraphView.prototype.installListeners = function(){
var graph = this.graph;
var container = graph.container;
universalGraph = graph; //this way, you will have access from .index.html to the graph object and you will be able to import the xml as shown in the example here https://stackoverflow.com/questions/56664578/mxgraph-save-functionality-not-working-locally/56735153#56735153 

As you will see, now the solution with the global Graph.xml was proposed here is redundant since now this way you will have always access into the graph object :) .

2) Since you have now the graph object, you can use it for any purpose you wany (export/import the xml state)

EDIT

I post also an example of how you should upload the xml in index.html

function uploadXML(){
        let xml = '<thexml_you_got_from_the_export><thexml_you_got_from_the_export/>';
        let doc = mxUtils.parseXml(xml);
        let codec = new mxCodec(doc);
        codec.decode(doc.documentElement, universalGraph.getModel());
        let elt = doc.documentElement.firstChild;
        let cells = [];
        while (elt != null)
        {
            let cell = codec.decode(elt)
            if(cell != undefined){
                if(cell.id != undefined && cell.parent != undefined && (cell.id == cell.parent)){
                    elt = elt.nextSibling;
                    continue;
                }
                cells.push(cell);
            }
            elt = elt.nextSibling;
        }
        universalGraph.addCells(cells);

    }
NickAth
  • 1,089
  • 1
  • 14
  • 35
  • Thanks @NickAth ! I can upload my own xml with your example but how can i have current xml to export it please ?This solution works : https://stackoverflow.com/questions/56899522/how-to-get-the-xml-from-mxgrpah-diagram but click event is not good for my project. – Cyri1 Jan 10 '20 at 12:32
  • i changed listener to mouseMove , it works but im not sure if it is the best way to do it – Cyri1 Jan 10 '20 at 13:03
  • What exactly you want to do? When you want your xml to be exported? – NickAth Jan 10 '20 at 13:38
  • I have a custom export button to export xml to database. – Cyri1 Jan 10 '20 at 21:19
  • This doesn't seem to work for me @NickAth, I keep getting duplicate ID error – Faisal Ahmed Farooq Jun 04 '20 at 19:29