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.
Installation
You can install PydanticXML with pip:
pip install pydantic-xmlmodel
Usage
To use PydanticXML, you need to import the XMLModel
class from the pydanticxml
module:
from pydantic_xmlmodel import XMLModel
Here is the class diagram of the library:
Examples
Defining a Model
You can define a model by subclassing XMLModel
and defining attributes with type annotations:
class AnimalCharacteristics(XMLModel):
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 dicttoxml()
method:
class Cat(XMLModel):
animal_characteristics: AnimalCharacteristics
name: str = "Kitty"
cat = Cat(animal_characteristics=AnimalCharacteristics())
print(cat.dicttoxml(indent=4))
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 fromxml()
method:
xml = """<?xml version="1.0" ?>
<cat name="Kitty">
<animalcharacteristics color="black" is_friendly="true" weight="10"/>
</cat>
"""
cat = Cat.fromxml(xml)
print(cat)
This will output:
animal_characteristics=AnimalCharacteristics(color='black', weight=10, is_friendly=True, __xml_content__=None) name='Kitty' __xml_content__='\n '
(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.1.1.tar.gz
.
File metadata
- Download URL: pydantic_xmlmodel-0.1.1.tar.gz
- Upload date:
- Size: 5.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.4.2 CPython/3.11.3 Linux/6.2.15-300.fc38.x86_64
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 30cd60fadc7c7cb615683473ee62f1c58a78b79cf5314f24f96f01126d348b93 |
|
MD5 | 0c479935f45bad2f37be5b02ec0c9e7d |
|
BLAKE2b-256 | 0e7d7b0e652418c8eab982057c681f908926812c222523deae05c1e08073506f |
File details
Details for the file pydantic_xmlmodel-0.1.1-py3-none-any.whl
.
File metadata
- Download URL: pydantic_xmlmodel-0.1.1-py3-none-any.whl
- Upload date:
- Size: 6.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.4.2 CPython/3.11.3 Linux/6.2.15-300.fc38.x86_64
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4ecdba7faf0f567843857db59f3d37a06ca85ffd23e34caed574ba060eead2ba |
|
MD5 | 6e38fcec46906de40c73aafe14e3cbb2 |
|
BLAKE2b-256 | b9a9fc8beb48ab2b04eb6526dc5992f3d32cee2aec00fac7872b5877f6593b5f |