iTranslated by AI
How to Fix Errors When Importing AI-Generated .drawio.svg Files (reading 'getAttribute')
Intro
When I want to generate diagrams like class diagrams or directory structures for existing source code, I'd like to rely on Copilot to make things easier. Furthermore, if I can output them in SVG format, it will pair well with Markdown, simplifying the documentation process.
Therefore, if I have Copilot read the directories and then format that as draw.io (which supports exporting diagrams in XML) and output it in the .drawio.svg format, it should work as expected.
However, when generating the entire process using tools like GPT-4.1, the outputted .drawio.svg file sometimes fails to load.
Environment
- vscode + copilot (GPT-4.1)
- vscode extension (Draw.io Integration: hediet.vscode-drawio: 1.9.0)
Error Details
When loading the created .drawio.svg, errors like the following occur:
Could not add object for mxGeometry
Cannot read properties of null (reading 'getAttribute')
Cause
First, let's focus on cases where the created file has the following format:
<?xml version="1.0" encoding="UTF-8"?>
<svg ...>
<mxfile host="app.diagrams.net">
<diagram ...>
<mxGraphModel ...>
<root>
<mxCell id="0" />
<mxCell id="1" parent="0" />
......
</root>
</mxGraphModel>
</diagram>
</mxfile>
</svg>
In this state, an error occurs, and even if you view it as an SVG file, it just displays an empty diagram.
By checking a correct .drawio format for reference, it seems it saves the content of the mxfile used in this file. So, I tried extracting the mxfile section and saving it, which allowed draw.io to load it. Thus, the content of mxfile seems correct, and the behavior appears to change based on the presence or absence of the svg tag.
<?xml version="1.0" encoding="UTF-8"?>
- <svg ...>
<mxfile host="app.diagrams.net">
...
</mxfile>
- </svg>
Solution
It seems that the content of mxfile can function even when placed in the content attribute of the svg tag. Therefore, I changed the method of embedding the data.
<?xml version="1.0" encoding="UTF-8"?>
+ <svg ... content="base64 encoded mxfile data">
- <mxfile>
- </mxfile>
</svg>
This displayed correctly in drawio.
However, just doing this means it is readable in drawio but empty as an SVG, so you also need to output the same rect tags, etc. Therefore, you need to adjust the prompt to instruct it to output rect tags as well (or simply re-save it in drawio).
Prompt
Create a hierarchical diagram of the src directory.
* Generate it as an SVG editable in draw.io (.drawio.svg)
* Embed the base64-encoded draw.io XML (the entire mxfile node) into the content attribute of the SVG tag
* Also include tags that can be displayed as SVG (like rect) within the SVG tag
This works, but it can be unstable with just the prompt.
If the diagram is too large, it seems to fail during the base64 encoding itself.
To stabilize this, it might be better to use something like SKILL.md.
For Agent Skills, using https://github.com/jgraph/drawio-mcp/tree/main/skill-cli allows for stable output.
Discussion