Format Code Now
/
ConvertXML to Python Next: XML to TypeScript ›

XML to Python Class Generator

CV-85

Paste an XML document and press Convert to get dataclasses, TypedDicts or Pydantic models. The XML is read into JSON first, so attributes, text and repeated elements follow the rules below, and it runs in your browser.

Runs in your browser. Nothing is uploaded.

XML

Python

How to use it

  1. Paste an XML document, or open an .xml file. Use one that shows every element you expect, with repeating elements repeated.
  2. Choose dataclass, TypedDict or Pydantic BaseModel (v2). Leave typed values on to read 59.90 as a number and true as a boolean, or turn it off to keep every value as text.
  3. Press Convert, then copy the result or download it as a .py file.
  4. Load the data with json.loads; Pydantic models check it with Root.model_validate(data), while dataclasses and TypedDicts describe its shape. Feed it the JSON form of your XML, not the XML itself.

How the classes are built

By default each object becomes a dataclass; a TypedDict or a Pydantic v2 model can be chosen instead. A key that is not usable as a name, such as first-name or class, is renamed, and the original key is kept in a comment (dataclass), in the functional TypedDict form, or in a Pydantic Field alias. Types are written as list[...] and Optional[...], numbers become int or float, and a key that is null in some items becomes Optional. In a dataclass, a key missing from some objects gets = None and fields without a default are placed first; mixed types and empty arrays become Any.

How the XML becomes classes

The root element becomes the single top-level key, so the first type holds one member named after it. An attribute becomes a key that starts with @, and text that sits next to attributes or child elements becomes #text. An element that repeats under the same parent becomes a list, while an element that appears once stays a single value. In Python, @id becomes id, with the key "@id" noted in a comment (or alias, or TypedDict key), and a clash with a child element of the same name gets a suffix.

What a sample cannot tell it

The result only reflects the sample you paste: a key that never appears has no member, and a number that is always whole in the sample is typed as whole even if other data holds decimals. Dates stay str; convert them with datetime, or let Pydantic parse them if you change the type. An element that appears once in your sample is written as a single value; if real documents can repeat it, paste a sample where it appears at least twice so it becomes a list.

Questions people ask

How do I convert XML to a Python class?

Paste the XML into the input box, choose your options and press Convert to get the classes. Load the data with json.loads; Pydantic models check it with Root.model_validate(data), while dataclasses and TypedDicts describe its shape. The classes follow the JSON form of the XML. The conversion happens in your browser.

How are keys that are not valid Python names handled?

A key such as first-name or class is renamed to a valid name. A dataclass notes the original key in a comment, a TypedDict switches to its functional form with the exact key, and Pydantic adds a Field alias with populate_by_name so both names work. This applies to the JSON form of your XML.

Why is a Python field Optional, and when does it get a None default?

A key that is null in some items becomes Optional[...]. A key missing from some objects also gets = None in a dataclass, and fields without defaults are placed first because Python requires that order. This applies to the JSON form of your XML.

Do the Python dataclasses convert nested dictionaries into classes?

Not by themselves: Root(**data) leaves nested objects as plain dicts. Pydantic models validate and build the nested classes for you, so choose that style if you want nested objects converted when you load data. This applies to the JSON form of your XML.

Are numbers in XML text typed as numbers?

By default, yes: text that is exactly a number, such as 59.90 or 3, becomes a number, and true or false becomes a boolean. Text with a leading zero or plus sign, like 007, stays a string. Turn the option off to make every value a string. The Python classes are written from that converted data.

What happens to XML attributes in the generated code?

Each attribute becomes a key that starts with @, such as @id, so it is a member of the generated type next to the child elements. The member name drops the @, and the original key is kept in the code where the language allows it. The Python classes are written from that converted data.

About this tool

The XML is read first and turned into the same JSON that the XML to JSON converter writes, and the code is generated from that. The root element becomes the single top-level key, so the root class holds one field named after it. An attribute becomes a key that starts with @, text next to attributes or child elements becomes #text, an element that appears more than once under one parent becomes a list, and one that appears once stays a single value, so a sample with one item gives a single object where another sample would give a list. Comments, processing instructions and the DOCTYPE are dropped. With the typed-values option on (the default), text that is exactly a number or true/false becomes a number or boolean; text with a leading zero or plus sign stays a string, and turning the option off makes every value a string. The result describes the converted JSON, not an XmlSerializer or JAXB mapping. Every object becomes a dataclass by default, or a TypedDict or Pydantic v2 model if you pick one. Keys that are valid Python names are used as they are; others are renamed, and the original key is kept by the TypedDict functional form, by a Pydantic alias, or by a comment in a dataclass. Types are written as list[...] and Optional[...], numbers become int or float, and a key that is null or missing in some items is Optional with a None default. Mixed types, always-null keys and empty arrays become Any. Everything runs in your browser; nothing is uploaded.

Format Code Now: free, no ads, no accounts. Your input stays in your browser.