PydanticXML
PydanticXML is a Python library that provides a way to convert Pydantic models to XML and vice versa. It is built on top of the Pydantic library and extends it with XML-specific functionality.
Features
- Based on Pydantic 2
- Convert Pydantic models to XML and vice versa
- Support for XML attributes, elements, content, lists.
- Compatible with most Pydantic features (e.g. default values, aliases, validators,
Fieldetc.) - Very easy to use. Extends Pydantic's API with only a few methods.
- Has no dependencies other than Pydantic
- Fully tested (almost 100% coverage)
- Supports mypy
Installation
You can install PydanticXML with pip:
pip install pydantic-xmlmodel
Usage
To use PydanticXML, you need to import the BaseModelXML class from the pydanticxml module:
from pydantic_xmlmodel import BaseModelXML
Or if your models is not based on BaseModelXML class you can use these functions:
from pydantic_xmlmodel import model_dump_xml, model_validate_xml
Examples
Defining a Model
You can define a model by subclassing BaseModelXML and defining attributes with type annotations:
class AnimalCharacteristics(BaseModelXML):
color: str = "black"
weight: int = 10
is_friendly: bool = True
Converting a Model to XML
You can convert a model to XML by calling the model_dump_xml() method:
class Cat(BaseModelXML):
animal_characteristics: AnimalCharacteristics
name: str = "Kitty"
cat = Cat(animal_characteristics=AnimalCharacteristics())
print(cat.model_dump_xml())
This will output:
<?xml version="1.0" ?><Cat name="Kitty"><AnimalCharacteristics color="black" weight="10" is_friendly="True" /></Cat>
Converting XML to a Model
You can convert XML to a model by calling the model_validate_xml() method:
xml = '<?xml version="1.0" ?><Cat name="Kitty"><AnimalCharacteristics color="black" weight="10" is_friendly="True" /></Cat>'
cat = Cat.model_validate_xml(xml)
print(cat)
This will output:
xml_content=None animal_characteristics=AnimalCharacteristics(xml_content=None, color='black', weight=10, is_friendly=True) name='Kitty'
(Note that the xml_content attribute is not part of the model. It is used to store the XML content, like this <element> xml_content <other_element /></element>)
Metadata
Release files for pydantic-xmlmodel 0.4
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| pydantic_xmlmodel-0.4.tar.gz | 5.8 kB | Without provenance | Details for pydantic_xmlmodel-0.4.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| pydantic_xmlmodel-0.4-py3-none-any.whl | Python 3 | none | any | Details for pydantic_xmlmodel-0.4-py3-none-any.whl |
Total release size: 13.0 kB
Release files / pydantic_xmlmodel-0.4.tar.gz
| Download URL | pydantic_xmlmodel-0.4.tar.gz |
|---|---|
| Size | 5.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
dd23315ae37d87ff4c0e86569035e9d47fd9cb19430835da16ec9a98b8c089aa
|
|
BLAKE2b-256 checksum How to use checksums |
5719fee6cb3d6867064b85a3e76346049152604e5e407a2dbe533f2b6fb858c7
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.7.0 CPython/3.8.18 Linux/6.5.11-300.fc39.x86_64
|
Release files / pydantic_xmlmodel-0.4-py3-none-any.whl
| Download URL | pydantic_xmlmodel-0.4-py3-none-any.whl |
|---|---|
| Size | 7.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
c0a5e7f3403d0cad79f2d7f0ea9a786bcf04f9e5df5eef81b567f881b589fbcd
|
|
BLAKE2b-256 checksum How to use checksums |
711959e51ec6b3f5cc8ae6a2fe0c0ccc7bb46c9199e0672ecde3f44042f4d662
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.7.0 CPython/3.8.18 Linux/6.5.11-300.fc39.x86_64
|
Security for pydantic-xmlmodel 0.4
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.