Python to XML Converter
Paste a Python class, dict, or dataclass. Get clean XML back.
Input
Output
Paste your Python code on the left and press Convert.
Or drop a file on this box, , or .
What this tool does
You have a Python model and a system on the other end that wants XML: a SOAP endpoint behind zeep, a vendor feed, a test fixture for a parser you are writing. Building the expected document with xml.etree.ElementTree means a SubElement call per field, and the nested list is always the one you forget. Paste the model and an instance instead, press Convert, and our backend returns well-formed XML. It reads a dataclass, a Pydantic model or a plain dict literal.
Python values are translated by type. Decimal("49.99") is written as the text 49.99, True and False drop to lowercase, a datetime becomes an ISO-8601 string and an Enum member contributes its .value. A None leaves an empty element, so shipping_address=None still shows up. A list, tuple or set gets a wrapper named after the attribute with a child per element, the kind of nesting you would get from xmltodict's unparse or a hand-written lxml builder. Snake_case names are kept as they are.
Names follow your code. The root element takes the class name, Order, and each field keeps its Python spelling, so customer_name is not camel-cased behind your back. On a Pydantic model, Field(alias="CustomerName") changes the tag; on a dataclass, field(metadata={"xml": "CustomerName"}) does the same. Dict keys that are not valid XML names, like "unit price" or "2fa", are cleaned up so the document still parses. Methods and attributes prefixed with an underscore, such as _cache, are left out of the result.
How to use it
Two buttons, three steps. Here is what happens to a models.py-style paste at each one.
Paste your Python (or try the sample)
Paste the code into the left editor with its imports. Load Sample gives you three dataclasses (OrderItem, Address, Order) and an order = Order(...) instance using Decimal prices, which is the shape the converter handles best: definitions plus one populated object.
Leave the imports, decorators, and type hints in. You do not need to strip them: the parser reads typing annotations and uses them to decide output types.
Hit Convert
Click the green Convert button. The source is posted to our backend, which reads the class definitions and the constructor call without executing anything, then sends back the XML. Expect a short wait behind the "Converting..." overlay.
Copy the XML
The XML shows up on the right, indented two spaces, opening with an XML declaration. Load it back with ElementTree.fromstring() in a test to confirm it parses, or paste it into the zeep request you are debugging.
When this actually comes in handy
SOAP / legacy integration fixtures
You have a Pydantic request model and need an XML body to send to a legacy SOAP service. Paste the model, get the XML, paste it into your test runner: done.
Turning dataclasses into config
A Settings dataclass with 30 fields becomes a ready-to-edit XML config template for any XML-based settings store, no handwritten scaffolding, no typos.
Keeping XSD docs honest
Generate XML samples for your README, API reference, or XSD-backed schema docs straight from the actual models in your code, so the examples never drift from reality.
Seeding integration tests
Convert instance literals from your pytest fixtures into XML seed files for systems that still speak XML: legacy APIs, mock servers, or XSLT pipelines.
Common questions
Does it work with dataclasses, Pydantic, and plain dicts?
Yes, all three. @dataclass and Pydantic BaseModel get their field names used as element names. Plain dict literals work too: the keys become element names and values are emitted by type. Nested models or dicts are expanded as nested elements.
How does it handle Decimal, datetime, and UUID?
Decimal("49.99") becomes 49.99 as element text. datetime, date, and time come out as ISO-8601 strings. UUID becomes a standard 8-4-4-4-12 hex string. Enum subclasses emit their .value. Sets become container elements just like lists.
What about Optional[...] and None values?
An attribute set to None stays in the document as a self-closing tag, for example <shipping_address/>, instead of vanishing. Keeping it means every record has the same elements, which matters when a downstream XSD marks the element as required.
Can I rename elements or push fields to attributes?
Renaming, yes. On Pydantic models Field(alias="x") renames the element, and on dataclasses field(metadata={"xml": "x"}) does the same. There is no setting that turns a field into an attribute: every field comes out as a child element, so move any you need into attributes by hand. Private fields prefixed with an underscore (_cache, _tmp) are dropped by default.
Can I paste multiple classes at once?
Yes. Anything under the backend's 64 KB request limit is fine. If there is an instance in the paste, that object becomes the document and the classes it references are nested inside it. If there are only class definitions, the converter writes a template with defaults (empty text, 0, false, empty lists), and unrelated top-level classes are grouped under a single <Root> element. Methods and attributes starting with an underscore are left out.
Is my code stored?
Pressing Convert sends the Python to our conversion backend and the XML is returned in the response; the site does not keep it. Settings modules are the usual risk here, since SECRET_KEY, database URLs and API tokens tend to live right next to the models, so cut them out before pasting.
Other tools you may need
Once you have XML from your dataclasses, you may want the reverse direction, the JSON version, or a second look at the output: