Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/465.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript mxGraph codec.decode不向实际图形添加单元格_Javascript_Xml_Mxgraph - Fatal编程技术网

Javascript mxGraph codec.decode不向实际图形添加单元格

Javascript mxGraph codec.decode不向实际图形添加单元格,javascript,xml,mxgraph,Javascript,Xml,Mxgraph,我正在尝试加载XML文件并将其转换为mxGraph,但是当我调用编解码器时,它不会更新我的图形 这是我的函数,其中容器是我的图形所在的div: function loadXml(container, xml) { // Checks if the browser is supported if (!mxClient.isBrowserSupported()) { // Displays an error message if the browser is not s

我正在尝试加载XML文件并将其转换为
mxGraph
,但是当我调用编解码器时,它不会更新我的图形

这是我的函数,其中容器是我的图形所在的div:

function loadXml(container, xml)
{
   // Checks if the browser is supported
   if (!mxClient.isBrowserSupported())
   {
      // Displays an error message if the browser is not supported.
      mxUtils.error('Browser is not supported!', 200, false);
   }
   else
   {
     // Creates the graph inside the given container
     var graph = new mxGraph(container);

     // Adds rubberband selection to the graph
     new mxRubberband(graph);

     var doc = mxUtils.parseXml(xml);
     var codec = new mxCodec(doc);
     codec.decode(doc.documentElement, graph.getModel());
   }
};

PS:我检查了
doc.documentElement
,它似乎是正确的。

由于使用Webpack的exports loader导入mxGraph,我出现了同样的问题,但我相信在其他情况下可能会发生

mxCodec.prototype.decode(doc,graphModel)
函数期望通过
窗口[objectName]
访问器在全局范围内提供所有mxGraph对象/构造函数

如果mxGraph封装在模块中,则对象不在全局范围内,解码失败


要确认这是问题所在,请在mxGraph的
mxCodec.prototype.decode
函数中添加断点并加载页面。然后,您可以验证是否可以找到对象。

我知道这个问题很老了,但我将为社区提供答案。您可以使用
mxUtils.parseXml(xml)
函数解析xml字符串并从中取出单元格,然后只需将单元格导入
mxGraph
对象,代码如下所示:

// Creates the graph inside the given container
var graph = new mxGraph(container);
let doc = mxUtils.parseXml(xml);
let codec = new mxCodec(doc);
codec.decode(doc.documentElement, graph.getModel());
let elt = doc.documentElement.firstChild;
let cells = [];
while (elt != null)
{  
    let cell = codec.decode(elt)
    if(cell != undefined){
        if(cell.geometry != undefined){
            if(cell.id != undefined && cell.parent != undefined && (cell.id == cell.parent)){
                elt = elt.nextSibling;
                continue;
            }
            cells.push(cell);
        }
    }
    elt = elt.nextSibling;
}
graph.addCells(cells);

这应该可以做到:)

我也有同样的问题:(