
Security News
Meet Socket at Black Hat and DEF CON 2025 in Las Vegas
Meet Socket at Black Hat & DEF CON 2025 for 1:1s, insider security talks at Allegiant Stadium, and a private dinner with top minds in software supply chain security.
PydanticXML is a Python library that provides a way to convert Pydantic models to XML and vice versa.
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.
Field
etc.)You can install PydanticXML with pip:
pip install pydantic-xmlmodel
To use PydanticXML, you need to import the BaseModelXML
class from the pydanticxml
module:
from pydantic_xmlmodel import BaseModelXML
Or if your models is not based on BaseModelXML class you can use these functions:
from pydantic_xmlmodel import model_dump_xml, model_validate_xml
You can define a model by subclassing BaseModelXML
and defining attributes with type annotations:
class AnimalCharacteristics(BaseModelXML):
color: str = "black"
weight: int = 10
is_friendly: bool = True
You can convert a model to XML by calling the model_dump_xml()
method:
class Cat(BaseModelXML):
animal_characteristics: AnimalCharacteristics
name: str = "Kitty"
cat = Cat(animal_characteristics=AnimalCharacteristics())
print(cat.model_dump_xml())
This will output:
<?xml version="1.0" ?><Cat name="Kitty"><AnimalCharacteristics color="black" weight="10" is_friendly="True" /></Cat>
You can convert XML to a model by calling the model_validate_xml()
method:
xml = '<?xml version="1.0" ?><Cat name="Kitty"><AnimalCharacteristics color="black" weight="10" is_friendly="True" /></Cat>'
cat = Cat.model_validate_xml(xml)
print(cat)
This will output:
xml_content=None animal_characteristics=AnimalCharacteristics(xml_content=None, color='black', weight=10, is_friendly=True) name='Kitty'
(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>
)
FAQs
PydanticXML is a Python library that provides a way to convert Pydantic models to XML and vice versa.
We found that pydantic-xmlmodel demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Meet Socket at Black Hat & DEF CON 2025 for 1:1s, insider security talks at Allegiant Stadium, and a private dinner with top minds in software supply chain security.
Security News
CAI is a new open source AI framework that automates penetration testing tasks like scanning and exploitation up to 3,600× faster than humans.
Security News
Deno 2.4 brings back bundling, improves dependency updates and telemetry, and makes the runtime more practical for real-world JavaScript projects.