FormatterHQ

XML to Python Dataclass Generator

Generate Python dataclasses from XML. Attributes and child elements become typed fields, repeated elements become List fields, and element text becomes value.

Input XML
Output PYTHON
Ready.

How to use the XML to Python

  1. Paste your XML document into the Input editor, or use Sample, Upload file or Load URL, or press Sample to see an example.
  2. Leave Top-level class name empty to name the root type after the root element, or type your own name.
  3. Tick Types only (no helpers) for bare dataclasses, or leave it off to include from_dict / to_dict helpers.
  4. Press Generate (it also updates as you edit), then use Copy or Download.

XML to Python dataclasses

Python's standard library parses XML with xml.etree.ElementTree, but the result is a tree of generic Element objects accessed through find() and get() calls. This XML to Python generator derives typed dataclasses from a sample document, so you can load the data once and work with attributes like config.database.port afterwards, with editor completion and type checking.

Example: a configuration file

<config env="production">
  <database host="db.internal" port="5432">
    <name>shop</name>
    <pool>20</pool>
  </database>
  <feature enabled="true">search</feature>
  <feature enabled="false">beta-checkout</feature>
</config>
@dataclass
class Database:
    name: str
    pool: int
    host: str
    port: int

@dataclass
class Feature:
    value: str
    enabled: bool

@dataclass
class Config:
    database: Database
    feature: List[Feature]
    env: str

The <feature> elements have both an attribute and text, so Feature gets an enabled field for the attribute and a value field for the text. Attribute values such as port="5432" are typed from their content, here int.

How the XML is mapped

The root element (<config>) names the top-level class, unless you enter your own name. Attributes and child elements are merged into the same class, so a field could come from either; check the XML when you write parsing code. Namespace prefixes are dropped, the XML declaration is ignored, and an empty element such as <notes/> becomes an empty string. Because an element that appears in some records but not others becomes Optional, pasting a document with several varied records gives the most accurate classes.

Filling the dataclasses from ElementTree

import xml.etree.ElementTree as ET

root = ET.fromstring(xml_text)
db = root.find("database")
config = Config(
    database=Database(name=db.findtext("name"), pool=int(db.findtext("pool")),
                      host=db.get("host"), port=int(db.get("port"))),
    feature=[Feature(value=f.text, enabled=f.get("enabled") == "true")
             for f in root.findall("feature")],
    env=root.get("env"),
)

ElementTree returns every value as a string, so conversions like int() and the boolean comparison are needed. The from_dict helpers generated in full mode expect a dictionary shaped like the tool's own XML-to-object mapping (attributes as plain keys, element text under value); libraries such as xmltodict use different conventions (@attr and #text), so rename keys if you use them.

Security and accuracy notes

  • For XML from untrusted sources, parse with defusedxml, which protects the standard parsers against entity-expansion attacks.
  • Repeated elements are only detected as lists if the sample contains at least two of them.
  • Numeric-looking text such as ZIP codes loses leading zeros when typed as int; switch those fields to str.

Format the generated module with the Python Formatter. To inspect the XML's structure first, use the XML Viewer, or convert it with XML to JSON. Everything runs in your browser.

Frequently asked questions

Can the generated dataclasses parse XML directly?
No. Dataclasses only describe the structure. Parse the XML with ElementTree or lxml and create the objects, as in the example.
How are attributes represented?
Attributes become ordinary fields next to the fields for child elements. Element text on an element with attributes becomes a value field.
Is ElementTree safe for untrusted XML?
The Python documentation recommends defusedxml for untrusted data, which guards the standard XML parsers against malicious documents.
Why is a repeated element not a list?
The sample contained it only once. Add a second occurrence so the generator can see that it repeats.

Last updated September 26, 2026

Related tools

JSON to Python Generate Python dataclasses with type hints from a JSON sample instantly, with nested types detected automatically. XML to Ruby Generate Ruby Dry::Struct classes from a XML sample instantly, with nested types detected automatically. XML to Rust Generate Rust structs with Serde derives from a XML sample instantly, with nested types detected automatically. XML to Scala Generate Scala case classes from a XML sample instantly, with nested types detected automatically. XML to Swift Generate Swift Codable structs from a XML sample instantly, with nested types detected automatically. XML to TypeScript Generate TypeScript interfaces from a XML sample instantly, with nested types detected automatically. GraphQL to TypeScript Generate TypeScript interfaces, enums and union types from a GraphQL schema (SDL), with nullability and argument types. JSON Schema to TypeScript Turn JSON Schema into TypeScript interfaces and types with optional properties, enums, $ref definitions and JSDoc comments. JSON to C++ Generate C++ classes with nlohmann::json helpers from a JSON sample instantly, with nested types detected automatically. JSON to Crystal Generate Crystal classes with JSON mapping from a JSON sample instantly, with nested types detected automatically. JSON to C# Generate C# classes with JsonProperty attributes from a JSON sample instantly, with nested types detected automatically. JSON to Dart Generate Dart classes with fromJson and toJson from a JSON sample instantly, with nested types detected automatically.