Skip to main content

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:

Graph]

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


Download files

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

Source Distribution

pydantic_xmlmodel-0.1.0.tar.gz (5.0 kB view details)

Uploaded Source

Built Distribution

pydantic_xmlmodel-0.1.0-py3-none-any.whl (5.8 kB view details)

Uploaded Python 3

File details

Details for the file pydantic_xmlmodel-0.1.0.tar.gz.

File metadata

  • Download URL: pydantic_xmlmodel-0.1.0.tar.gz
  • Upload date:
  • Size: 5.0 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

Hashes for pydantic_xmlmodel-0.1.0.tar.gz
Algorithm Hash digest
SHA256 cafd1075ace3c9d07bb0cee0b40674ffbe14f08e337d6db59f424a9585698ddf
MD5 b9d533d96c4c526b65600a6aacc78e59
BLAKE2b-256 c421c4b982b4d25291ae9202645a6a496fdaebfb102af73bce4de47154cfe79f

See more details on using hashes here.

File details

Details for the file pydantic_xmlmodel-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: pydantic_xmlmodel-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 5.8 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

Hashes for pydantic_xmlmodel-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 230ebd62bb5b209bae8a3b624e575ad8f990b04ecafca579560ef8c5e3792e19
MD5 2c7771fc6abeb967c6775ea46f0a6bdf
BLAKE2b-256 bc0e7407353c061232746a724a571ee0c2ba6e376c25e577329555fe6935ebf5

See more details on using hashes here.

Supported by

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