MxCodec解码功能不适用于XML [英] MxCodec decode function not working with XML
问题描述
我正在使用Angular7和MxGraph库在javascript中开发应用程序.我可以使用以下代码将MxGraph模型保存为XML:
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);
但是,按照API的指示,我无法对其进行解码.我也曾尝试粘贴XML并尝试解码它,但没有成功,像这样:
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());
在返回的模型中没有 Node0
的痕迹.
In the returned model there is no trace of Node0
.
推荐答案
我很抱歉回答自己的答复,但最终在这里找到了一个线索:将mxGraph用作npm模块时未创建编辑器
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;
这篇关于MxCodec解码功能不适用于XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!