MxCodec decode function not working with XML

可紊 提交于 2020-01-16 14:11:14

问题


I'm developing an application in javascript with Angular7 and MxGraph library. I am able to save the MxGraph model into an XML with this code:

var enc = new mx.mxCodec(mx.mxUtils.createXmlDocument());
var node = enc.encode(editor.graph.getModel());
this.xml = mx.mxUtils.getPrettyXml(node);

But I'm not able to decode it, following APIs indications. I have also tried to paste the XML and try to decode it without success like this:

// Usually I would do this:
// var doc = mx.mxUtils.parseXml(this.xml);

// Just for testing purposes I did this
var doc = mx.mxUtils.parseXml(
           '<mxGraphModel><root><mxCell id="0"/><mxCell id="1" parent="0"/>'+
           '<Node0 label="Input" style="container" id="input"><mxCell style="container" vertex="1" connectable="0" parent="1">'+
           '<mxGeometry y="20" width="100" height="580" as="geometry"/></mxCell></Node0>'+
           '</root></mxGraphModel>');

var dec = new mx.mxCodec(doc);
dec.decode(doc.documentElement, graph.getModel());
console.log(graph.getModel());

In the returned model there is no trace of Node0.


回答1:


I apologize for answering my own reply, but I finally found a clue here: Editor doesn't created when mxGraph is used as npm module

Adding these lines to my code solved the issue:

// Workaround because window['mxGraphModel'] is not defined
window['mxEditor'] = mx.mxEditor;
window['mxGeometry'] = mx.mxGeometry;
window['mxDefaultKeyHandler'] = mx.mxDefaultKeyHandler;
window['mxDefaultPopupMenu'] = mx.mxDefaultPopupMenu;
window['mxGraph'] = mx.mxGraph;
window['mxCell'] = mx.mxCell;
window['mxCellPath'] = mx.mxCellPath;
window['mxGraph'] = mx.mxGraph;
window['mxStylesheet'] = mx.mxStylesheet;
window['mxDefaultToolbar'] = mx.mxDefaultToolbar;
window['mxGraphModel'] = mx.mxGraphModel;


来源:https://stackoverflow.com/questions/58804525/mxcodec-decode-function-not-working-with-xml

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!