XML to Python Dataclass Generator

Dataclasses plus from_element parsing code for ElementTree

XML Input

Python Output

Paste your XML on the left and the Python code shows up here.

Or drop a file on this box, , or .

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.

1

Paste a representative sample

Paste the XML into XML Input, or pick a file with Upload. The two <line> elements in the sample are what make line a List[Line] read with el.findall(); a single one would be read with el.find() instead. 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.

2

Pick the class style

Root class renames the outer class; leave it empty and <invoice> becomes Invoice. Use dataclasses decides between @dataclass with field(default_factory=list) and a plain class with an explicit __init__. typing.Optional / List writes Optional[str] and List[Line]; untick it for the built-in str | None and list[Line] style.

3

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 are read with el.get("currency") and become fields next to the children. When an element has both, as <total currency="EUR">120.00</total> does, it gets its own class with currency plus a value field filled from _to_float(el.text).

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 what the sample holds. quantity becomes int and goes through _to_int(), unitPrice becomes float, paid becomes bool, and issuedOn stays str because a date is not a number. Zero-padded values like 00731 are kept as strings.

Is the XML uploaded?

No. The dataclasses are generated by JavaScript running in your browser, and the XML is not posted to any server. The input cap is 256 KB, and an invoice with two lines covers every field the classes need.

Related tools

Other converters you might reach for next: