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

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 to_xml() method:

class Cat(XMLModel):
    animal_characteristics: AnimalCharacteristics
    name: str = "Kitty"

cat = Cat(animal_characteristics=AnimalCharacteristics())
print(cat.to_xml(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 from_xml() method:

xml = """<?xml version="1.0" ?>
<cat name="Kitty">
    <animalcharacteristics color="black" is_friendly="true" weight="10"/>
</cat>
"""
cat = Cat.from_xml(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.2.1.tar.gz (4.8 kB view details)

Uploaded Source

Built Distribution

pydantic_xmlmodel-0.2.1-py3-none-any.whl (5.7 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: pydantic_xmlmodel-0.2.1.tar.gz
  • Upload date:
  • Size: 4.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.5.0 CPython/3.11.3 Linux/6.3.4-201.fc38.x86_64

File hashes

Hashes for pydantic_xmlmodel-0.2.1.tar.gz
Algorithm Hash digest
SHA256 6a9c820439c83ad456cf078a57af7482eded14a4a6e80351284bd9382fa16072
MD5 1b33850fb22115183a6b3fbc55f0f1ad
BLAKE2b-256 a2bd9cd72e87375273053ed2fef5be8e7c6229e5b76c0b1dc50bba4a106e5c4f

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pydantic_xmlmodel-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 5.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.5.0 CPython/3.11.3 Linux/6.3.4-201.fc38.x86_64

File hashes

Hashes for pydantic_xmlmodel-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 56a0355af61d45cf1923261d22b84028adddc3705c5342d4012bb126bec2e860
MD5 6813732887954bb4dbb68645c4680a2a
BLAKE2b-256 ad07a17bf328ff4dd240976360b5ec3370a1d6b032e42dc2b9ca5549c316f986

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