CalcCafe

XML to Python

Paste XML and instantly get typed Python @dataclass definitions inferred from the document structure.

Example

Input XML:

<book id="1">
 <title>Dune</title>
 <pages>412</pages>
</book>

Output Python:

@dataclass
class Root:
  id: int
  title: str
  pages: int

How it works

It parses the XML with the browser DOMParser, infers a type model from elements, attributes, repeated children and text values, then emits idiomatic Python dataclasses with the root type named Root.

Good to know

XML to Python turns a sample XML document into ready-to-paste Python @dataclass definitions, guessing each field's type from the actual values in your markup. It's aimed at developers who need to model an XML API response, config file, or data feed in Python without hand-writing class after class. Paste a representative snippet, click Convert, and you get a complete module that already imports dataclass, field, List, and Optional.

Reach for it when you're scaffolding parsing code, drafting type hints for a new integration, or just want a quick structural overview of an unfamiliar XML payload. Because everything runs in your browser, it's also safe to throw in real production XML you can't paste into an online service. The root element is always emitted as a class named Root, so you'll typically rename that to match your domain after generating.

To read the output: attributes and simple text elements become scalar fields typed as int, float, bool, or str; elements that repeat turn into List[...] with default_factory=list; and any element that has its own children or attributes becomes a separate dataclass referenced by a quoted forward reference. If a field appears in some instances of a repeated element but not others, it's marked Optional[...] = None so the generated class stays flexible.

One practical caveat: the inference is only as good as your sample, so paste XML that covers the variety you expect. A few specifics worth knowing:

Frequently asked questions

How are XML attributes and text handled?
Attributes become snake_case fields on the dataclass, typed by their value (int, float, bool, str). When an element has both attributes and text content, the text is exposed as a 'text' field.
How are repeated elements and nested structures typed?
Repeated child elements become List[...] fields with default_factory=list, and nested elements that have their own children or attributes get their own dataclass referenced by a quoted forward reference.
Is my data uploaded anywhere?
No — this tool runs entirely in your browser. Your input never leaves your device and it works offline once loaded.
Is it free?
Yes, completely free with no sign-up and no limits.

People also ask

Does the XML to Python converter support namespaces and prefixed tags?
It reads each element by its local name, so namespace prefixes are effectively stripped when building field and class names. The structure is preserved, but you may need to add namespace handling yourself if your parsing code must distinguish identically named tags from different namespaces.
What Python version do the generated dataclasses require?
Dataclasses require Python 3.7 or later. The output uses typing.List and typing.Optional, which work on 3.7+, though on newer versions you could also rewrite them as list[...] and the | None syntax.
How does it decide between int, float, bool, and str?
It inspects the trimmed text value: 'true' or 'false' (any case) becomes bool, whole numbers become int, numbers containing a decimal point or exponent become float, and anything else, including empty values, becomes str.
Can it convert XML to a Pydantic model instead of a dataclass?
No, this tool only emits standard library @dataclass definitions. You can adapt the output to Pydantic by changing the class decorator to inherit from BaseModel and adjusting the field defaults.
Why is my root class named Root instead of the actual XML tag?
The converter always names the top-level type Root regardless of the document element's tag. Rename it manually after generating if you want it to match your XML root element.
How are repeated child elements with differing fields handled?
Repeated elements are merged into a single List of one dataclass. Fields that appear in every occurrence stay required, while fields missing from some occurrences become Optional with a default of None.
Is there a way to convert Python dataclasses back into XML?
This tool only goes from XML to Python; it does not reverse the process. Serializing dataclasses back to XML would require separate code, for example using xml.etree.ElementTree to build elements from each field.
Does pasting XML here send my data to a server?
No, the conversion happens entirely in your browser using the built-in DOMParser, and the page works offline once loaded. Your XML never leaves your device.

Related tools