domToWorkspace doesn't seem to do anything

41 views
Skip to first unread message

Seb Palmer

unread,
Aug 9, 2021, 7:05:45 AM8/9/21
to Blockly
Hello

Grateful for any help!  I am doing something wrong here, trying to load some xml into a workspace, hoping that the blocks will appear in place in the UI:

const xml = `<xml>
    <block type="start" id="1" deletable="false" x="30" y="30">
        <next>
            <block type="move_forwards" id="2">
                <next>
                    <block type="move_forwards" id="3"></block>
                </next>
            </block>
        </next>
    </block>
</xml>`;

var p = new DOMParser();
var blocks = p.parseFromString(xml, "application/xml");
var workspace = Blockly.mainWorkspace;

var result = Blockly.Xml.domToWorkspace(blocks, workspace);

// and result is just [], and nothing changes in the UI

I'm on a learning curve with Blockly, so it might be something simple but thanks for any help


Beka Westberg

unread,
Aug 10, 2021, 6:44:41 PM8/10/21
to Blockly
Hello,

Thanks for your question :D It looks like you've got your XML set up correctly! But there may be other issues that I can't see from this code sample :/ Here are a few things I would check:

1) Is my workspace injected? (from your description it seems like yes, but still good to check!)
2) Are my "start" and "move_forward" blocks defined? You can check this in the browser console by doing `console.log(Blockly.Blocks)`
3) Are there any other errors in the browser console?

If you're able to share a link to your source code we may be able to take a look at that as well =) But hopefully that gives you some places to start!

Best wishes,
--Beka

Seb Palmer

unread,
Aug 11, 2021, 7:54:06 AM8/11/21
to Blockly
Hi Beka 

Thank you so much.  The issue turned out to be that the DOMParser().parseFromString returns something different than Blockly.Xml.textToDom

This works:


            const xml = `{solution}`;
            Blockly.Xml.clearWorkspaceAndLoadFromXml(Blockly.Xml.textToDom(xml), Blockly.mainWorkspace);
            

Beka Westberg

unread,
Aug 11, 2021, 9:50:52 AM8/11/21
to blo...@googlegroups.com
Sweet! I'm glad it's working for ya :D If you have any further questions always feel free to ask =)

Best of luck on your project!
--Beka

--
You received this message because you are subscribed to the Google Groups "Blockly" group.
To unsubscribe from this group and stop receiving emails from it, send an email to blockly+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/blockly/89b96922-a78f-4d7a-9dfe-e35b677cb7f8n%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages