pyd-xml
Easily serialize Pydantic models to and from XML --- with native support for attributes, nested elements, lists, optional fields, and type validation.
This library provides a natural XML integration layer for Pydantic models, allowing full round-trip serialization between Python objects and XML data.
📦 Installation
pip install pyd-xml
🚀 Quick Start
Define your XML-serializable models by extending XMLModel and using
xml_field to control XML mapping:
from pyd_xml import XMLModel, xml_field
class Employee(XMLModel):
id: int = xml_field(attr=True) # Attribute
role: str | None = xml_field(include_none=True) # Optional field included as empty tag
name: str
salary: float
class Department(XMLModel):
name: str
employees: list[Employee]
class Company(XMLModel):
id: int = xml_field(attr=True)
name: str
founded: int
departments: list[Department]
Serialize your model to XML:
company = Company(
id=1001,
name="TechCorp",
founded=2005,
departments=[
Department(
name="Engineering",
employees=[
Employee(id=1, name="Alice", role="Developer", salary=70000),
Employee(id=2, name="Bob", role=None, salary=68000),
],
),
Department(
name="HR",
employees=[
Employee(id=3, name="Eve", role="HR Manager", salary=60000),
],
),
],
)
xml_str = company.to_xml_str()
print(xml_str)
Output:
<Company id="1001">
<name>TechCorp</name>
<founded>2005</founded>
<departments>
<name>Engineering</name>
<employees id="1">
<name>Alice</name>
<role>Developer</role>
<salary>70000</salary>
</employees>
<employees id="2">
<name>Bob</name>
<role/>
<salary>68000</salary>
</employees>
</departments>
<departments>
<name>HR</name>
<employees id="3">
<name>Eve</name>
<role>HR Manager</role>
<salary>60000</salary>
</employees>
</departments>
</Company>
🔁 Full Round Trip
Easily convert back from XML to Python objects:
decoded = Company.from_xml(xml_str)
assert decoded == company
⚙️ Utility Functions
You can also use the standalone helpers:
from pyd_xml import model_to_xml, xml_to_model
element = model_to_xml(company)
company2 = xml_to_model(Company, element)
🧩 Dictionary Representation
Convert models into a dictionary representation with XML semantics:
company_dict = company.to_xml_dict()
print(company_dict["Company"]["@id"]) # Attribute
print(company_dict["Company"]["name"]) # Element
License
Licensed under the Apache License 2.0.
---
Author
Mohamed Tahri <`simotahri1@gmail.com`>
Metadata
Release files for pyd-xml 0.1.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| pyd_xml-0.1.1.tar.gz | 17.5 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| pyd_xml-0.1.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 32.8 kB
Release files / pyd_xml-0.1.1.tar.gz
| Download URL | pyd_xml-0.1.1.tar.gz |
|---|---|
| Size | 17.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
38b455f53e0aa18d9f9120beae7150ea85e2672a4247c204d4b931ad698633dc
|
|
BLAKE2b-256 checksum How to use checksums |
b28d34753ee645b43a6f8560b8d5da73a3e8fa82825d7c8128bd020a7bee01b7
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.13.7
|
Release files / pyd_xml-0.1.1-py3-none-any.whl
| Download URL | pyd_xml-0.1.1-py3-none-any.whl |
|---|---|
| Size | 15.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
5b685f9244881571f5c84b7400a684f690398c12f2e30f105cf47a28a74e3bfb
|
|
BLAKE2b-256 checksum How to use checksums |
b5ae293a4248a31d207a3977a8a188737f5b5e46dbeee64d11934ca481dc8303
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.13.7
|