XMLScripts

59 views
Skip to first unread message

Noor HM

unread,
Jul 31, 2025, 10:16:11 AMJul 31
to Blockly
Hello Blockly Team,
Hope your week is going great,

I've been checking some documentation regarding the XML scripts, as I'm currently working on some tool that generates this script then i would parse it into blocks (via my app or just the blockly advanced playground by importing it directly). I found this documentation and i wanted the team verify if it's outdated or needs any update since it seems like a reliable knowledge source to provide into the tool. Additionally, i notice that sometimes when i import, it wouldn't parse the full XML script and just parse like a part of it, this could be because some tags has the same ID or for any other reason, would really appreciate more tips on how to make sure the XML is valid and can be parsed/serialized.

Christopher Allen

unread,
Jul 31, 2025, 12:11:33 PMJul 31
to blo...@googlegroups.com
Hi Noor,

I've been checking some documentation regarding the XML scripts, as I'm currently working on some tool that generates this script then i would parse it into blocks (via my app or just the blockly advanced playground by importing it directly). I found this documentation and i wanted the team verify if it's outdated or needs any update since it seems like a reliable knowledge source to provide into the tool.

Having had a quick look, it appears that the information in that document remains an accurate description of the XML serialisation system—but a few years ago we added a new, JSON-based serialisation system that is the preferred way to save and load Blockly workspaces, and we strongly encourage app developers to use this new API, which will continue to be updated as required as Blockly adds new features, while the XML-based API will be maintained in its current state to provide backwards compatibility for existing apps that may have extensive collections of user-created programs stored in the legacy XML format.
 
Additionally, i notice that sometimes when i import, it wouldn't parse the full XML script and just parse like a part of it, this could be because some tags has the same ID or for any other reason, would really appreciate more tips on how to make sure the XML is valid and can be parsed/serialized.

My first recommendation would be to use the JSON serialisation if at all possible.  Failing that, I would suggest that you run your generated XML string through the DOMParser DOMParser parseFromString method and check to make sure that the returned object tree has the expected structure.


Best wishes,

Christopher

Noor HM

unread,
Aug 4, 2025, 11:47:27 AMAug 4
to Blockly

Hello,
I have adopted that approach and it is working fine so far, however i noticed an odd behavior, lets say i have a blockly program in json format which happen to contain some variables, when i serialize it, the variables of the program gets created successfully but not utilized inside the blocks, instead it would create some generalized variables instead and utilize those, what could be issue here? 
I'm just using the Blockly.serialization.workspaces.load() normally
For example the variable created in the Json is "num", but it is not being used as needed and it's been replaced by "i" and "j"

Screenshot 2025-08-04 120945.png

Noor Alhamoud

unread,
Aug 5, 2025, 6:24:26 AMAug 5
to blo...@googlegroups.com
Hello,
Please let me know if this can be resolved by another function call or what exactly.

Thanks Blockly Team,


--
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 visit https://groups.google.com/d/msgid/blockly/82959dd3-c9a2-4b78-81ee-272001392113n%40googlegroups.com.

Christopher Allen

unread,
Aug 5, 2025, 8:15:05 AMAug 5
to blo...@googlegroups.com
Hi Noor,

I have adopted that approach and it is working fine so far, however i noticed an odd behavior, lets say i have a blockly program in json format which happen to contain some variables, when i serialize it, the variables of the program gets created successfully but not utilized inside the blocks, instead it would create some generalized variables instead and utilize those, what could be issue here? 
I'm just using the Blockly.serialization.workspaces.load() normally
For example the variable created in the Json is "num", but it is not being used as needed and it's been replaced by "i" and "j"

Screenshot 2025-08-04 120945.png


I recreated your program in the Blockly playground, using the variable num instead of i and j, and then saved it to JSON, which gave me the serialised state that I have attached to this message as example.json; I was then able to successfully reload this JSON and the program was recreated with num as expected:

Screenshot 2025-08-05 at 13.04.21.png

The playground's save button does this (paraphrased) to serialise the state of the workspace:

        var state = Blockly.serialization.workspaces.save(workspace);
        var json = JSON.stringify(state, null, 2);

That produces JSON that includes a variable model for num:

{
  "blocks": {
    "languageVersion": 0,
    "blocks": [
      {
        "type": "variables_set",
        // …additional detail omitted.
        "fields": {
          "VAR": {
            "id": "[v?.wo/07ZqKU5^ym%T|"
          }
        },
        // 
…additional blocks omitted.
      }
    ]
  },
  "variables": [
    {
      "name": "num",
      "id": "[v?.wo/07ZqKU5^ym%T|"
    }
  ]
}

The load button then does this (paraphrased) to deserialize the state of the workspace:

          var state = JSON.parse(json);
          Blockly.serialization.workspaces.load(state, workspace);

No additional calls should be required to make saving and loading variables work.

Can you check that your code is doing something similar, and also check which version of Blockly you are using?  It's possible that at some point in the past there was a bug involving serialisation of variables that was subsequently fixed.


Best wishes,

Christopher

example.json

Noor Alhamoud

unread,
Aug 5, 2025, 11:32:17 AMAug 5
to blo...@googlegroups.com
Thanks for replying,

I'm currently using blockly version 12.0.0
this is the json originally 

--
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.
test.json

Christopher Allen

unread,
Aug 5, 2025, 12:23:10 PMAug 5
to blo...@googlegroups.com
Hi Noor,

I'm currently using blockly version 12.0.0

OK, that version should be fine.
 
this is the json originally

Ah ha.  I believe I see the problem.  Compare this excerpt from your JSON (reformatted and excerpted):

{
  "blocks": {
    "languageVersion": 0,
    "blocks": [
      {
        "type": "variables_set",
        "fields": {
          "VAR": "num"
        },
        // …additional blocks omitted.
      }
    ]
  },
  "variables": [
    {
      "name": "num",
      "type": "",
      "id": "num"
    }
  ]
}

with the corresponding sections from the example I gave previously:

{
  "blocks": {
    "languageVersion": 0,
    "blocks": [
      {
        "type": "variables_set",
        // …additional detail omitted.
        "fields": {
          "VAR": {
            "id": "[v?.wo/07ZqKU5^ym%T|"
          }

        },
        // 
…additional blocks omitted.
      }
    ]
  },
  "variables": [
    {
      "name": "num",
      "id": "[v?.wo/07ZqKU5^ym%T|"
    }
  ]
}

Note the difference in how the variable for the variable field is specified.  Try changing it to explicitly specify "VAR": ("id": "num"} and see if that solves the problem.

Best wishes,

Christopher

Reply all
Reply to author
Forward
0 new messages