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-converter 0.0.13
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| pydantic_xml_converter-0.0.13.tar.gz | 4.9 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| pydantic_xml_converter-0.0.13-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 9.6 kB
Release files / pydantic_xml_converter-0.0.13.tar.gz
| Download URL | pydantic_xml_converter-0.0.13.tar.gz |
|---|---|
| Size | 4.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
f0b5c279fb6103a850b99e39db3e7bf77636f97c487740cdf79781ac4cb393b6
|
|
BLAKE2b-256 checksum How to use checksums |
f47a9dea655c8e101b216aa51a467a3fc645b5367b31d39e328a71fd1a90527b
|
| 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_converter-0.0.13-py3-none-any.whl
| Download URL | pydantic_xml_converter-0.0.13-py3-none-any.whl |
|---|---|
| Size | 4.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
8647b1f213e13998a6ce7a8292f46a18c93f029afc0ddc94c1022d1b37453f40
|
|
BLAKE2b-256 checksum How to use checksums |
0bbdf7c4a8ff29a183d16bb9b3d9c973f403d89d9c5832167b118b18ab9f72df
|
| 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-converter 0.0.13
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.