Skip to main content

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.

Metadata

Release files for pydantic-xml-extension 0.0.12

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pydantic-xml-extension 0.0.12
File Size Uploaded Provenance
pydantic_xml_extension-0.0.12.tar.gz 4.9 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for pydantic-xml-extension 0.0.12
File Interpreter ABI Platform
pydantic_xml_extension-0.0.12-py3-none-any.whl Python 3 none any Details

Total release size: 9.6 kB

Release files / pydantic_xml_extension-0.0.12.tar.gz

Download URL pydantic_xml_extension-0.0.12.tar.gz
Size 4.9 kB
Tags Source
SHA-256 checksum
How to use checksums
169ea4fffa802eafc7e96b6e3c075d2afa99866b01c19ef0eaad6168f1ca6591
BLAKE2b-256 checksum
How to use checksums
23462242fdc8db05af305df0d94de0a8d1e09b131de80c7f265d15fef98508d4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.3.0 CPython/3.11.1 Linux/6.0.12-300.fc37.x86_64

Release files / pydantic_xml_extension-0.0.12-py3-none-any.whl

Download URL pydantic_xml_extension-0.0.12-py3-none-any.whl
Size 4.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
a2913ad227ac26dac674c88d6e57135c9bab5e34ef6f867498db456688949d28
BLAKE2b-256 checksum
How to use checksums
1721bd289d30b14ab674537cea47da8faf87de73e07a7ac14d633eeb6772814c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.3.0 CPython/3.11.1 Linux/6.0.12-300.fc37.x86_64

Security for pydantic-xml-extension 0.0.12

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page