WebToolsPlanet
Converter Tools

XML to Python Converter

Paste XML and generate Python models in dataclass, pydantic, TypedDict, or attrs style.

Last updated: May 28, 2026

Client-Side Processing
Input Data Stays on Device
Instant Local Execution

Find this tool useful? Support the project to keep it free!

Buy me a coffee

What is XML to Python Converter?

XML data often needs to be represented as Python classes before it can be validated, transformed, or passed through an application. This converter normalizes an XML sample into a JSON-like object shape, then generates Python types from that structure.

Attributes can be preserved, repeated sibling elements become lists, nested elements become nested classes, and simple text values can be inferred as int, float, bool, or str. Choose the Python model style that matches your project: @dataclass for lightweight value objects, pydantic for runtime validation, TypedDict for dict-based typing, or attrs for attrs-based models.

How to Use XML to Python Converter

1

Paste XML into the input panel.

2

Choose a Python output style.

3

Set a root class name or leave it blank to use the XML root element.

4

Choose Python 3.10+ or Python 3.9 type syntax.

5

Toggle XML attributes, scalar inference, and optional fields.

6

Copy the generated Python code.

Common Use Cases

  • Creating pydantic models from XML API samples.
  • Bootstrapping dataclasses for XML feed processing.
  • Adding TypedDict hints to code that handles XML-derived dictionaries.
  • Migrating XML fixtures into typed Python test data.
  • Documenting XML response shapes with Python classes.

Example Input and Output

An XML order with repeated item elements becomes Python classes with list fields.

XML input
<order id="A100"><item><quantity>2</quantity></item></order>
Python output
from dataclasses import dataclass


@dataclass
class Item:
    quantity: int


@dataclass
class Order:
    id_: str
    item: list[Item]

Privacy

All conversion happens in your browser. XML input is not sent to a server.

XML namespaces

Namespace prefixes are preserved in parsed XML names by the browser parser. Review generated field names when your XML uses namespaces.

Frequently Asked Questions

How are repeated XML elements represented?
Repeated sibling elements with the same tag name become Python list fields, such as list[Item] or List[Item].
What happens to XML attributes?
When attribute support is enabled, attributes are normalized into fields before Python generation. Python-safe names are produced automatically.
Should I choose dataclass or pydantic?
Use dataclass for lightweight containers. Use pydantic when you need runtime validation, alias handling, and parsing behavior.
Can inferred types be wrong?
Yes. Type inference is based on the XML sample. Review important fields before using generated code as a production model.
Does this upload my XML?
No. XML parsing and Python generation run locally in your browser.