XML to Python Converter Online — Generate Dataclasses

Paste any XML and generate typed Python @dataclass models. Attributes carry field(metadata=...) markers so xmltodict, lxml, and dataclass-wizard know which fields originated as XML attributes.

What is an XML to Python converter?

An XML to Python converter reads a sample XML document and emits typed Python @dataclass definitions ready to receive parsed data from xmltodict, lxml, or ElementTree. The OpenFormatter generator inspects every element and attribute and produces a complete class hierarchy with type hints, list types for repeating elements, and metadata flags so it's clear which fields came from XML attributes.

JSON-to-class generators only deal with key-value pairs. XML adds attributes, namespaces, and mixed content — and a Python model that ignores the attribute/element distinction loses information. The generator below tags each attribute field with field(metadata={xml_attr: ...}) so xmltodict's @attr dict keys map cleanly onto your dataclass.

Sample XML and the Python dataclass it generates

Input XML

<book id="bk101" lang="en">
  <title>Effective Python</title>
  <year>2019</year>
  <price currency="USD">39.99</price>
</book>

Generated Python

from dataclasses import dataclass, field
from typing import List, Optional

@dataclass
class Price:
    currency: str = field(metadata={'xml_attr': 'currency'})
    text: float = field(metadata={'xml_text': True})

@dataclass
class Book:
    id: str = field(metadata={'xml_attr': 'id'})
    lang: str = field(metadata={'xml_attr': 'lang'})
    title: str
    year: int
    price: Price

Note id and lang are flagged as XML attributes; title and year are plain elements. The nested <price currency="USD">39.99</price> generates a Price dataclass with one attribute field and one text-content field — the same shape xmltodict returns.

How to convert XML to Python — 4 steps

  1. Paste your XML. An RSS item, a SOAP response body, an Ant build file — anything well-formed.
  2. Click Convert. The browser parses the XML and emits typed dataclasses with attribute metadata.
  3. Verify the attribute fields. Confirm that field(metadata={xml_attr: ...}) appears on the right fields.
  4. Wire up xmltodict. Parse XML with xmltodict.parse(xml) then map the dict into the dataclass — by hand, or via dacite.from_dict.

Type-Hinted Dataclasses

Every field has a Python type hint — str, int, float, bool, List[T], or a nested dataclass. mypy / pyright pick up errors the moment you misuse a value.

Attribute Metadata

XML attributes carry field(metadata={'xml_attr': name}) so xmltodict's @attr keys can be mapped programmatically without losing the XML semantics.

Client-Side Only

Conversion runs in your browser. No XML — including SOAP responses with credentials — is uploaded. Verify in DevTools: zero network requests on Convert.

Common use cases

  • check_circleGenerate Python dataclasses from an RSS or Atom feed sample
  • check_circleBuild typed models for a SOAP service response
  • check_circleConvert XML configuration into typed dataclasses for a CLI tool
  • check_circleGenerate models for a sitemaps.xml or robots.xml processor
  • check_circleBuild dataclasses for a SAML assertion or WS-Security payload
  • check_circleCreate models for legacy enterprise XML in a data pipeline
  • check_circleGenerate dataclasses for JUnit XML test reports
  • check_circleBuild typed models for OPDS or DITA documentation XML

Why client-side conversion matters

SOAP and enterprise XML payloads frequently embed customer PII, internal identifiers, and signed tokens. Many security policies forbid pasting them into hosted converters. OpenFormatter parses XML and generates Python entirely in JavaScript on your machine — no upload, no cookies, no logs. Open DevTools → Network and confirm: clicking Convert fires zero requests.

More XML tooling

Validate, format, or convert XML to other languages — every tool is browser-side.

Frequently Asked Questions

How does the converter handle XML attributes vs elements?

XML attributes (e.g. <book id="bk101">) become dataclass fields tagged with field(metadata={"xml_attr": "id"}). Element children become plain typed fields. The metadata makes the attribute origin explicit so xmltodict (which prefixes attribute keys with "@") and dataclass-wizard can both populate the model from the parsed dict.

Can I use lxml or xmltodict with the generated dataclasses?

Both work. xmltodict.parse(xml) returns a nested dict where attributes are keyed "@attr" and text content under "#text" — map those into the dataclass with a small from_dict helper or dataclass-wizard. With lxml, walk the ElementTree and call YourClass(**fields) per element. The generated metadata documents which fields came from attributes.

What Python version is required?

Dataclasses require Python 3.7+. The generated code uses typing.List and typing.Optional which work on any 3.7+ version. On Python 3.9+ you can replace List[X] with list[X] and Optional[X] with X | None for shorter syntax.

Does the output handle XML namespaces?

Element names are stripped of namespace prefixes by default (so soap:Body becomes body). To preserve namespace info, edit the metadata to add an xml_ns key, or read namespaces directly from element.tag in lxml. xmltodict preserves namespaces in keys when process_namespaces=False.

How are repeated elements handled?

When the converter sees the same child element twice (<tag>python</tag><tag>programming</tag>), the field becomes List[Tag] or List[str] depending on whether the children have their own structure. For single-occurrence elements this is just the leaf type — re-run the converter against XML where the field repeats to see the array typing.

Why use dataclasses instead of pydantic?

Dataclasses are stdlib (no extra dependency) and lighter at runtime. If you want validation and JSON Schema generation, copy the field declarations into a pydantic BaseModel or use pydantic.dataclasses.dataclass — same field syntax, same metadata, plus pydantic features.

Are elements with attributes and text content modeled?

Yes. <price currency="USD">39.99</price> generates a Price class with currency: str (xml_attr metadata) and text: float (xml_text metadata). xmltodict represents the same structure as {"@currency": "USD", "#text": "39.99"} which maps directly to the two fields.

Is the XML I paste sent to your servers?

No. Parsing and code generation run entirely in JavaScript inside your browser via the built-in DOMParser. Open DevTools → Network and confirm: clicking Convert produces zero HTTP requests. Safe for SOAP responses, configuration files, or any XML containing secrets.

XML to Python Converter Online — Generate Dataclasses