Free XML to Python Class Generator Online
XML to Python classes for ElementTree or lxml code: one class per element, with @dataclass and typing hints you can switch on or off in the config panel.
XML Input
Python Output
What Is XML to Python?
When you're working with an XML API in Python, the typical workflow is: parse with xml.etree.ElementTree or lxml, then manually create a class to hold the data. This tool skips that step — paste your XML and get a ready-to-use Python class or @dataclass with all the right fields. Enable Use Typing to add Optional[str] hints, or Use Dataclass for the @dataclass decorator from Python's dataclasses module. The W3C XML specification defines the input format. Class generation is done by script on this page, so a SOAP payload full of customer records stays on your laptop.
Conversion runs in your browser. You can enable Use Dataclass for @dataclass decorators and Use Typing for type hints. Nothing is sent to a server. The generated classes are a starting point for Django, Flask, or other Python projects.
How to Use This Tool
Paste or Upload XML
Drop an XML document into the input pane, or load one from disk with Upload. The config panel lets you to set the root Class Name, Use Dataclass, and Use Typing. The more representative your sample, the better the generated structure.
Review the Python Output
The right panel shows generated Python classes. Use ElementTree or lxml to parse XML and map elements to these types. For dataclasses, you can add custom parsing logic.
Copy or Download
Once the fields look right, Copy the classes or save them as a .py module with Download. For JSON to Python, use JSON to Python. For XML formatting, use XML Formatter.
XML to Python Examples
Here is an example of generating Python classes from XML.
Example: Subscriber record
XML input:
Generated Python output:
When XML to Python Helps
Use this tool when integrating APIs that return XML, when parsing RSS feeds, SOAP responses, or when building Django or Flask apps that consume XML feeds. Manually writing classes for complex XML is tedious; this tool gives you a head start. Add parsing logic and adjust types as needed for your project.
Frequently Asked Questions
When should I use @dataclass instead of a regular class?
Use @dataclass (Python 3.7+) when you want automatic __init__, __repr__, and __eq__ with minimal boilerplate. Enable Use Dataclass and Use Typing in the config for fully typed dataclasses. Regular classes are useful when you need custom __init__ logic.
How do I parse XML into the generated class with ElementTree?
Use xml.etree.ElementTree: call tree = ET.parse('file.xml'), then read element text/attributes and populate your class manually. For complex schemas, lxml's objectify or Pydantic with XML adapters can help.
Can I use this with Django or Flask?
Yes. The generated classes are plain Python — drop them into any Django, Flask, or FastAPI project. They're especially useful for serializing XML API responses to Python objects in Python web applications.
Does my XML get uploaded when I generate Python classes?
No. The document is read with the browser's own DOMParser API and the classes are built in the same tab, so the XML is never posted anywhere.
Related Tools
For Python XML parsing, see ElementTree and lxml. For XML, see the W3C XML specification. For parsing in the browser, see MDN DOMParser. For dataclasses, see Python dataclasses. See also W3C XML and Pydantic.