Skip to main content

No project description provided

Project description

Pydantic XML Converter

Allows existing Pydantic models to be converted to/from XML with support for XML attributes.

Install

pip install pydantic-xml-extension

Examples

Generating XML from an existing Pydantic model

from pydantic import fields, BaseModel
from pydantic_xml import PydanticXmlConverter, XmlAttribute

class CustomBaseModel(BaseModel):
    class Config:
        allow_population_by_field_name = True
        
class ExistingModel(CustomBaseModel):
    name: Annotated[str, fields.Field(alias="Name")]
    age: int

model = ExistingModel(Name="test", age=12)
converter = PydanticXmlConverter("Model")
converter.set_xml_attribute("name", XmlAttribute(key="id", value="123"))
converter.set_xml_attribute("age", XmlAttribute(key="custom", value="value"))
print(converter.xml(model))
>> <?xml version="1.0" encoding="utf-8"?>
>> <Model><Name id="123">test</Name><age custom="value">12</age></Model>

Creating an instance of an existing Pydantic model from XML

from pydantic_xml import XmlBaseModel, XmlAttribute

class CustomBaseModel(BaseModel):
    class Config:
        allow_population_by_field_name = True

class ExistingModel(XmlBaseModel):
    name: Annotated[str, fields.Field(alias="Name")]
    age: int

input_xml = '<Model><Name id="123">test</Name><age custom="value">12</age></Model>'

converter = PydanticXmlConverter("Model")
model = converter.parse_xml(input_xml, ExistingModel)

print(model)
>> Model(name="test", age=12)

print(converter.generate_dict(model))
>> {"Name": "test", "age": 12}

print(converter.generate_dict(model, by_alias=False))
>> {"name": "test", "age": 12}

print(converter.generate_xml(model))
>> <?xml version="1.0" encoding="utf-8"?>
>> <Model><Name id="123">test</Name><age custom="value">12</age></Model>

to view or access the saved attributes identified during parsing, you use the converter.xml_attributes attribute.

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

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pydantic_xml_extension-0.0.12-py3-none-any.whl (4.7 kB view details)

Uploaded Python 3

File details

Details for the file pydantic_xml_extension-0.0.12.tar.gz.

File metadata

  • Download URL: pydantic_xml_extension-0.0.12.tar.gz
  • Upload date:
  • Size: 4.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.3.0 CPython/3.11.1 Linux/6.0.12-300.fc37.x86_64

File hashes

Hashes for pydantic_xml_extension-0.0.12.tar.gz
Algorithm Hash digest
SHA256 169ea4fffa802eafc7e96b6e3c075d2afa99866b01c19ef0eaad6168f1ca6591
MD5 2bcc56b7eb7c98510d22f9a820a0ec52
BLAKE2b-256 23462242fdc8db05af305df0d94de0a8d1e09b131de80c7f265d15fef98508d4

See more details on using hashes here.

File details

Details for the file pydantic_xml_extension-0.0.12-py3-none-any.whl.

File metadata

File hashes

Hashes for pydantic_xml_extension-0.0.12-py3-none-any.whl
Algorithm Hash digest
SHA256 a2913ad227ac26dac674c88d6e57135c9bab5e34ef6f867498db456688949d28
MD5 86544f0c334d8c183b48215a6bfc7640
BLAKE2b-256 1721bd289d30b14ab674537cea47da8faf87de73e07a7ac14d633eeb6772814c

See more details on using hashes here.

Supported by

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