PydanticXML is a Python library that provides a way to convert Pydantic models to XML and vice versa.
Project description
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,
Field
etc.) - 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>
)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file pydantic_xmlmodel-0.4.tar.gz
.
File metadata
- Download URL: pydantic_xmlmodel-0.4.tar.gz
- Upload date:
- Size: 5.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.7.0 CPython/3.8.18 Linux/6.5.11-300.fc39.x86_64
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | dd23315ae37d87ff4c0e86569035e9d47fd9cb19430835da16ec9a98b8c089aa |
|
MD5 | 2790a596f4e6621237a95c41a0b94eba |
|
BLAKE2b-256 | 5719fee6cb3d6867064b85a3e76346049152604e5e407a2dbe533f2b6fb858c7 |
File details
Details for the file pydantic_xmlmodel-0.4-py3-none-any.whl
.
File metadata
- Download URL: pydantic_xmlmodel-0.4-py3-none-any.whl
- Upload date:
- Size: 7.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.7.0 CPython/3.8.18 Linux/6.5.11-300.fc39.x86_64
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | c0a5e7f3403d0cad79f2d7f0ea9a786bcf04f9e5df5eef81b567f881b589fbcd |
|
MD5 | 850d973f09e0ae2c1d90bde908f7b88c |
|
BLAKE2b-256 | 711959e51ec6b3f5cc8ae6a2fe0c0ccc7bb46c9199e0672ecde3f44042f4d662 |