Jaco van der Laan

Learn how to extract structured metadata from draw.io diagrams using Python — enabling feedback loops, visual diffing, and hybrid workflows that bridge business input with technical design.

Summary

📥 Parse .drawio files into clean metadata rows

🔍 Extract shapes, connectors, coordinates, and labels

💡 Enrich your metadata store with business-drawn diagrams

🔄 Support round-trip flows: diagram → metadata → code

⚠️ Handle edge cases with grouping, styles, and annotations

🛠️ Store everything back in DuckDB for easy querying and reuse

Part 3: Closing the Loop — From Diagrams to Metadata

In the first two articles, we explored how to generate draw.io diagrams from metadata. Now, we’ll walk through the reverse: parsing .drawio diagrams back into structured metadata. This closes the loop — enabling collaboration between visual thinkers and metadata-driven developers.

🔄 Why Parse Diagrams Back?

While we advocate a code-first workflow, parsing diagrams is useful when:

  • Business users provide annotated diagrams
  • Teams start with conceptual diagrams
  • You want to extract layout, connections, or annotations
  • You want to enrich your metadata store with visual insights

This enables:

  • Re-synchronization of layout metadata
  • Feedback ingestion from stakeholders
  • Version comparison between diagram and code

Think of this process as “metadata harvesting” from visual sources

📁 What’s in a .drawio File?

Draw.io diagrams are XML-based files with:

  • mxCell elements for each shape, connector, or label
  • Nested mxGeometry tags for coordinates and dimensions
  • Style attributes for visual representation
  • ID references to indicate links

Example:

<mxCell id="1" value="Customer" style="shape=rectangle;" vertex="1">
<mxGeometry x="100" y="150" width="120" height="40" as="geometry"/>
</mxCell>
<mxCell id="2" value="Order" style="shape=rectangle;" vertex="1">
<mxGeometry x="300" y="150" width="120" height="40" as="geometry"/>
</mxCell>
<mxCell id="3" edge="1" source="1" target="2" style="endArrow=block;"/>

🛠️ Python-Based Parser: From .drawio to DuckDB

We can extract metadata from .drawio files using xml.etree.ElementTree or lxml.

Sample Code:

import xml.etree.ElementTree as ET
import duckdb
# Parse XML
tree = ET.parse('diagram.drawio')
root = tree.getroot()
mx_cells = root.findall('.//mxCell')
# Connect to DuckDB
con = duckdb.connect('metadata.duckdb')
con.execute("CREATE TABLE IF NOT EXISTS nodes (id TEXT, label TEXT, x FLOAT, y FLOAT, type TEXT)")
for cell in mx_cells:
if cell.get('vertex') == '1':
id = cell.get('id')
label = cell.get('value')
style = cell.get('style')
shape_type = style.split('=')[1].split(';')[0] if style else 'unknown'
geo = cell.find('mxGeometry')
x = float(geo.get('x', '0'))
y = float(geo.get('y', '0'))
con.execute("INSERT INTO nodes VALUES (?, ?, ?, ?, ?)", (id, label, x, y, shape_type))

🧱 Output Metadata Structure

![[Pasted image 20251003202659.png]]

To extract links:

  • Look for edge="1" cells
  • Match source and target attributes
  • Capture any arrow direction, labels, styles

✅ Use Cases

  • Reverse engineer mappings from business diagrams
  • Reposition elements in generated diagrams
  • Compare diagram and code-based definitions
  • Auto-generate YAML templates from business models

This step also enables hybrid workflows:

  • Business draws first
  • Engineers refine in YAML
  • Diagrams regenerate from YAML later

⚠️ Limitations & Tips

  • Labels in draw.io may include HTML or escape characters → clean before inserting
  • Groupings are stored as parent-child hierarchies (consider flattening)
  • Parsing layout vs semantic metadata may need separate passes
  • Some style/positioning conventions must be enforced upfront to keep it parseable

This method works best when the draw.io diagrams follow your team’s style guide.

🔄 Future Enhancement Ideas

  • Use graph libraries to extract more context from connectors
  • Validate extracted metadata against YAML or schema
  • Store visual metadata (e.g., color, position, group) in separate layers
  • Enable visual change detection with diagram diffs

📌 Summary

Parsing draw.io back into metadata gives you more flexibility, especially in collaborative environments. It’s not meant to replace code-first modeling — but to support a round-trip metadata flow.

  • ✅ Bring business feedback into code
  • ✅ Keep layout and logic in sync
  • ✅ Drive consistency in hybrid teams

In the next article, we’ll explore how to organize diagrams and metadata together in Visual Studio Code for seamless collaboration — and how to set up diagram-to-code linking for YAML and SQL.