XML to Python Dataclass Generator
Dataclasses plus from_element parsing code for ElementTree
XML Input
Python Output
What the generator writes
Paste an XML sample and you get a module with one dataclass per element name. Each class also gets a from_element classmethod that reads an xml.etree.ElementTree element into the object, so the output parses real documents, not just describes them.
Attributes are read with el.get(), children with el.find() and el.findall(), and namespaced names are written in ElementTree's {uri}local form so they match. Whole numbers go through int(), decimals float(), and true/false becomes a bool. Repeated elements become lists, and anything missing comes back as None. Snake_case names are used throughout; Python keywords get a trailing underscore.
The file starts with from __future__ import annotations, so class order does not matter and it runs on Python 3.7 and newer. We ran the generated code against the page sample before shipping it. The conversion itself happens in your browser; the XML you paste is not uploaded.
How to use it
Paste, pick a style, copy the module.
Paste a representative sample
Paste XML into XML Input or Upload a file. Show repeated elements at least twice so they become lists. Sample loads an invoice:
<invoice number="INV-2041" currency="USD">
<line sku="SKU-101"><quantity>2</quantity><unitPrice>24.99</unitPrice></line>
<line sku="SKU-205"><quantity>1</quantity><unitPrice>39.50</unitPrice></line>
<paid>false</paid>
</invoice>The output panel reports parse errors. Use the XML Formatter to make a one-line document readable first.
Pick the class style
Root class renames the top-level class (empty uses the root element name). Use dataclasses switches between @dataclass and plain classes with an __init__. typing.Optional / List writes Optional[str] and List[Line]; untick it for the built-in str | None and list[Line] style.
Copy or download
Download saves a .py module. The last line shows how to load a file: Invoice.from_element(ET.parse("invoice.xml").getroot()).
Where it helps
ETL and data pipelines
Turn supplier XML files into typed objects before loading them into pandas or a database, with the field mapping in one place.
Django and Flask integrations
Parse an incoming XML webhook into dataclasses, validate them, then map them onto your models.
Scripts and automation
Reading a CI report, sitemap or config file in a script gets shorter when the parsing code is already written.
Tests
Dataclasses compare by value, so you can assert that a parsed fixture equals an expected object in one line.
Common questions
Does the generated code really parse XML?
Yes. Every class has from_element(el), which reads attributes, child text and nested elements from an ElementTree element. It only uses the standard library.
Which Python version is needed?
3.7 or newer. The from __future__ import annotations line also makes the str | None style safe on versions older than 3.10, because annotations are not evaluated at runtime.
How are attributes and text mapped?
Attributes become fields like child elements. When an element has attributes and text, as in <price currency="USD">24.99</price>, the text goes into a value field.
What about namespaces?
Lookups use ElementTree's Clark notation, for example el.find("{urn:shop}item"), built from the namespace URIs in your sample. That keeps working even if a later document uses a different prefix.
How are numbers detected?
From your sample values: whole numbers become int, decimals float, true/false bool. A value with a leading zero, such as a zip code, stays a string.
Is the XML uploaded?
No. It is parsed in your browser. Inputs over 256 KB are rejected; a few records are enough to infer the classes.
Related tools
Other converters you might reach for next: