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)
2条回答 默认 最新
duanhe8280 2019-07-17 12:19关注Ok, similarly to How to get the XML from mxGrpah diagram? answer, it is not again clear to me how to get the
graphobject 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
graphobjectvar universalGraph;ii. Go into a .js file where the
graphobject has already been created (for example go to js/view/mxGraphView oninstallListenersfunction and assign the instance of the graph object to your global accessible referencemxGraphView.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#56735153As you will see, now the solution with the global
Graph.xmlwas proposed here is redundant since now this way you will have always access into thegraphobject :) .2) Since you have now the
graphobject, 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.htmlfunction 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); }本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报