Skip to main content

Easily serialize Pydantic models to and from XML.

Project description

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`>

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pyd_xml-0.1.1.tar.gz (17.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pyd_xml-0.1.1-py3-none-any.whl (15.3 kB view details)

Uploaded Python 3

File details

Details for the file pyd_xml-0.1.1.tar.gz.

File metadata

  • Download URL: pyd_xml-0.1.1.tar.gz
  • Upload date:
  • Size: 17.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for pyd_xml-0.1.1.tar.gz
Algorithm Hash digest
SHA256 38b455f53e0aa18d9f9120beae7150ea85e2672a4247c204d4b931ad698633dc
MD5 d0938bf043fb48019c475a08647e356e
BLAKE2b-256 b28d34753ee645b43a6f8560b8d5da73a3e8fa82825d7c8128bd020a7bee01b7

See more details on using hashes here.

File details

Details for the file pyd_xml-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: pyd_xml-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 15.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for pyd_xml-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 5b685f9244881571f5c84b7400a684f690398c12f2e30f105cf47a28a74e3bfb
MD5 83c89be6346625f0c27f0197b3639dc9
BLAKE2b-256 b5ae293a4248a31d207a3977a8a188737f5b5e46dbeee64d11934ca481dc8303

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page