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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pydantic_xml_converter-0.0.13.tar.gz.
File metadata
- Download URL: pydantic_xml_converter-0.0.13.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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f0b5c279fb6103a850b99e39db3e7bf77636f97c487740cdf79781ac4cb393b6
|
|
| MD5 |
002f7e7211751e023e932767b0299caf
|
|
| BLAKE2b-256 |
f47a9dea655c8e101b216aa51a467a3fc645b5367b31d39e328a71fd1a90527b
|
File details
Details for the file pydantic_xml_converter-0.0.13-py3-none-any.whl.
File metadata
- Download URL: pydantic_xml_converter-0.0.13-py3-none-any.whl
- Upload date:
- Size: 4.7 kB
- Tags: Python 3
- 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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8647b1f213e13998a6ce7a8292f46a18c93f029afc0ddc94c1022d1b37453f40
|
|
| MD5 |
89b340a26032e14055ab107977346a03
|
|
| BLAKE2b-256 |
0bbdf7c4a8ff29a183d16bb9b3d9c973f403d89d9c5832167b118b18ab9f72df
|