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 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.tar.gz (5.2 kB view details)

Uploaded Source

Built Distribution

pydantic_xmlmodel-0.2-py3-none-any.whl (6.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: pydantic_xmlmodel-0.2.tar.gz
  • Upload date:
  • Size: 5.2 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.tar.gz
Algorithm Hash digest
SHA256 63e05ba14d6dd5e1ff887f9538b616f02b7dfc063ac4070404663e9c173133a4
MD5 5f4194489e45abb1a0c35b9f50cffd74
BLAKE2b-256 c729f23281c3fe20003fbd82acc00660d248b9dbc1d152cc1822f2c3e9c880f7

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pydantic_xmlmodel-0.2-py3-none-any.whl
  • Upload date:
  • Size: 6.1 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-py3-none-any.whl
Algorithm Hash digest
SHA256 cba9dee73af43f4ca39ad056ce52f410303b2d11adf73c415560927bdfd23058
MD5 6d13e8a08a02990416fe4b332104bb11
BLAKE2b-256 8fdcbc5a23309401111637804b3ebf29d2892b84b33bc86d7ab435119f79108a

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