XML to Python

Generate Python dataclasses, Pydantic models or TypedDicts from an XML sample.

XML to Python – Typed models from XML documents

The XML to Python converter reads a sample XML document and produces Python classes that describe it: standard-library @dataclass classes with a from_dict helper, Pydantic models with field aliases, or TypedDict definitions for static type checking with mypy and Pyright.

How XML maps to types

XML has no native types, so the converter reads every element and attribute value and infers one: whole numbers become integers, decimals become floating-point numbers, true/false become booleans, and everything else stays a string. Attributes are kept apart from child elements (they appear as @name keys internally), an element that has both attributes and text gets a dedicated text field, and repeated sibling elements such as several item nodes become a list. Namespace prefixes are stripped from names so ns:customer and customer produce the same type.

Built for xmltodict

Most Python code reads XML with xmltodict.parse(), which uses exactly the same conventions as this tool: attributes become @name keys and element text next to attributes becomes #text. The generated Pydantic models therefore declare aliases such as Field(alias="@id"), and the dataclass from_dict reads data.get("@id"), so you can feed the dictionary from xmltodict straight into the models. Remember to enable xmltodict's force_list option for elements that can repeat, because a single occurrence would otherwise be returned as a dict instead of a list.

When to use it

Use it to wrap legacy SOAP services, RSS or Atom feeds, sitemap files, invoice formats such as UBL, or configuration files exported by older Java tools. Typed models give you autocompletion and catch misspelled element names before production does.

Tips

Paste a document that contains every optional element at least once, and include two or more repeated children so they are recognised as lists. Check numeric-looking identifiers such as postal codes or SKUs: values with leading zeros stay strings, but plain digits become int.

Inspect the structure first with the XML Viewer or convert the document with XML to JSON.

Frequently Asked Questions

No. The XML is parsed and converted by JavaScript running in your browser tab. Nothing is sent to a server, so internal feeds, SOAP payloads and configuration files stay on your machine.

The output uses from __future__ import annotations and typing.List/Optional, so it runs on Python 3.8 and newer. Pydantic output targets Pydantic v2 but also works with v1 for simple models.

Attributes become normal fields. For dataclasses and Pydantic the original key (for example @id) is kept so the models line up with xmltodict output; TypedDict lists them as comments because @id is not a valid identifier.

Namespace prefixes are removed from element and attribute names and xmlns declarations are ignored, so the generated field names stay short and readable.