XML to Python Converter
Paste XML and generate Python models in dataclass, pydantic, TypedDict, or attrs style.
Last updated: May 28, 2026
Find this tool useful? Support the project to keep it free!
Buy me a coffeeWhat 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
Paste XML into the input panel.
Choose a Python output style.
Set a root class name or leave it blank to use the XML root element.
Choose Python 3.10+ or Python 3.9 type syntax.
Toggle XML attributes, scalar inference, and optional fields.
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.
<order id="A100"><item><quantity>2</quantity></item></order>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.
Related Tools
JSON to Python Converter
Generate Python classes from a JSON example. Choose @dataclass, pydantic BaseModel, TypedDict, or attrs style. Targets Python 3.10+ or 3.9 type syntax.
XML to Dart Converter
Generate Dart classes from XML, including nested classes, List<T> fields for repeated elements, optional final fields, nullable types, and fromJson/toJson methods.
XML to Go Converter
Generate Go structs from XML, including nested structs, slices for repeated elements, optional XML attribute preservation, scalar inference, and struct tags.
XML to C# Converter
Generate C# classes from XML, including XmlRoot, XmlElement, XmlAttribute, lists for repeated elements, nullable properties, and optional namespace wrapping.
XML to JavaScript Converter
Convert XML into a JavaScript object literal, const declaration, ES module export, CommonJS export, or expression with optional attribute and scalar inference.
XML to JSON Converter
Parse and convert XML to clean JSON instantly — attributes, namespaces, and nested elements handled.

