Blockly.Xml.textToDom需要一个<xml>文档

时间:2019-11-06 14:00:37

标签: javascript xml blockly google-blockly

我一直在尝试从localStorage恢复所有使用过的块。我正在遍历所有本地保存的块,但是出现以下错误。如果能得到帮助,那太好了。

错误:

blockly_compressed.js:1274 Uncaught Error: Blockly.Xml.textToDom expected an 
<xml> document.
at Object.Blockly.Xml.textToDom (blockly_compressed.js:1274)
at restoreWorkspace ((index):312)
at HTMLButtonElement.onclick ((index):115)
Blockly.Xml.textToDom   @   blockly_compressed.js:1274
restoreWorkspace    @   (index):312
onclick @   (index):115

恢复所有块的代码:

 // restore blocks
    function restoreWorkspace() {
        for (var i = 0; i < localStorage.length; i++){
            let program=(localStorage.getItem(localStorage.key(i)));
            Blockly.mainWorkspace.clear();
            let textToDom = Blockly.Xml.textToDom(program);
            Blockly.Xml.domToWorkspace(Blockly.mainWorkspace, textToDom);
        }
    }

保存合成的代码:

   function saveWorkspace() {
        let xml = Blockly.Xml.workspaceToDom(Blockly.mainWorkspace);
        let domToPretty = Blockly.Xml.domToPrettyText(xml);
        var ranInt = Math.floor(Math.random() * 10);
        window.localStorage.setItem("myProgram"+ranInt, domToPretty);
    }

0 个答案:

没有答案