mxGraph: Create graph with XML

☆樱花仙子☆ 提交于 2019-12-03 20:28:18

try this

 var xml = '<root><mxCell id="2" value="World!Vishal" vertex="1"><mxGeometry x="200" y="150" width="80" height="30" as="geometry"/></mxCell><mxCell id="3" edge="1" source="2"><mxGeometry relative="1" as="geometry"><mxPoint x="440" y="90" as="targetPoint"/></mxGeometry></mxCell></root>';
                     var doc = mxUtils.parseXml(xml);
                    var codec = new mxCodec(doc);
                    var elt = doc.documentElement.firstChild;
                    var cells = [];
                    while (elt != null){                
                      cells.push(codec.decodeCell(elt));
                        graph.refresh();
                      elt = elt.nextSibling;
                    }

                this.graph.addCells(cells);

Let me know if you have any issue.

if you are using Grapheditor you can use its method (setGraphXml):

let doc = mxUtils.parseXml(xml);

myEditor.editor.setGraphXml(doc.documentElement);

where doc is the parsed XML and myEditor is your editor instance. you can get editor instance with :

const myEditor = new EditorUi(new Editor(urlParams['chrome'] == '1', themes));
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!