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.

Features

  • Convert Pydantic models to XML and vice versa
  • Support for XML attributes, elements, content, lists.
  • Compatible with most Pydantic features (e.g. default values, validators, Field etc.)
  • Very easy to use. Extends Pydantic's API with only a few methods.
  • Has no dependencies other than Pydantic
  • Have a good docs in the source code
  • Fully tested (100% coverage)

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

Uploaded Source

Built Distribution

pydantic_xmlmodel-0.3.4-py3-none-any.whl (7.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: pydantic_xmlmodel-0.3.4.tar.gz
  • Upload date:
  • Size: 6.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.6.1 CPython/3.11.4 Linux/6.4.14-200.fc38.x86_64

File hashes

Hashes for pydantic_xmlmodel-0.3.4.tar.gz
Algorithm Hash digest
SHA256 afb89a630af7b152ce2ab17ffd73539dd5f426972af03d63e008a34749fe38fb
MD5 3d17a6a6792161bf47b8bb0adf9db5ee
BLAKE2b-256 43729dcca8d6d6a89210b6a3216f935c48b6fd7961cfce0a1d3feca4ede7bb20

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pydantic_xmlmodel-0.3.4-py3-none-any.whl
  • Upload date:
  • Size: 7.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.6.1 CPython/3.11.4 Linux/6.4.14-200.fc38.x86_64

File hashes

Hashes for pydantic_xmlmodel-0.3.4-py3-none-any.whl
Algorithm Hash digest
SHA256 8c0f8c5969024a0029a2276752c42a0066f5fcd88e28e3cbd77ffd2e1fdbb69b
MD5 3bf7d09f2fec9675f64cf120c3dba81a
BLAKE2b-256 68605d2349bb23bff2ebaff2a4f900afc0e0873ad2229550fff3813ebd2ad605

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