Developer Interface#
pydantic xml serialization/deserialization extension
- class pydantic_xml.BaseXmlModel[source]#
Bases:
BaseModel
Base pydantic-xml model.
- classmethod model_rebuild(**kwargs)[source]#
Try to rebuild the pydantic-core schema for the model.
This may be necessary when one of the annotations is a ForwardRef which could not be resolved during the initial attempt to build the schema, and automatic rebuilding fails.
- Args:
force: Whether to force the rebuilding of the model schema, defaults to False. raise_errors: Whether to raise errors, defaults to True. _parent_namespace_depth: The depth level of the parent namespace, defaults to 2. _types_namespace: The types namespace, defaults to None.
- Returns:
Returns None if the schema is already “complete” and rebuilding was not required. If rebuilding _was_ required, returns True if rebuilding was successful, otherwise False.
- Parameters:
kwargs (Any) –
- Return type:
None
- classmethod from_xml_tree(root, context=None)[source]#
Deserializes an xml element tree to an object of cls type.
- classmethod from_xml(source, context=None)[source]#
Deserializes an xml string to an object of cls type.
- to_xml_tree(*, skip_empty=False, exclude_none=False, exclude_unset=False)[source]#
Serializes the object to an xml tree.
- to_xml(*, skip_empty=False, exclude_none=False, exclude_unset=False, **kwargs)[source]#
Serializes the object to an xml string.
- Parameters:
- Returns:
object xml representation
- Return type:
- model_config: ClassVar[ConfigDict] = {}#
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- class pydantic_xml.RootXmlModel(root=PydanticUndefined)[source]#
Bases:
RootModel[~RootModelRootType]
,BaseXmlModel
,Generic
[RootModelRootType
]Base pydantic-xml root model.
- Parameters:
root (RootModelRootType) –
- model_config: ClassVar[ConfigDict] = {}#
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
- pydantic_xml.attr(name=None, ns=None, *, default=PydanticUndefined, default_factory=PydanticUndefined, **kwargs)[source]#
Marks a pydantic field as an xml attribute.
- pydantic_xml.element(tag=None, ns=None, nsmap=None, nillable=False, *, default=PydanticUndefined, default_factory=PydanticUndefined, **kwargs)[source]#
Marks a pydantic field as an xml element.
- Parameters:
nsmap (Optional[Dict[str, str]]) – element xml namespace map
nillable (bool) – is element nillable. See https://www.w3.org/TR/xmlschema-1/#xsi_nil.
default (Any) – the default value of the field.
default_factory (Optional[Callable[[], Any]]) – the factory function used to construct the default for the field.
kwargs (Any) – pydantic field arguments. See
pydantic.Field
- Return type:
- pydantic_xml.wrapped(path, entity=None, ns=None, nsmap=None, *, default=PydanticUndefined, default_factory=PydanticUndefined, **kwargs)[source]#
Marks a pydantic field as a wrapped xml entity.
- Parameters:
entity (Optional[FieldInfo]) – wrapped entity
path (str) – entity path
nsmap (Optional[Dict[str, str]]) – element xml namespace map
default (Any) – the default value of the field.
default_factory (Optional[Callable[[], Any]]) – the factory function used to construct the default for the field.
kwargs (Any) – pydantic field arguments. See
pydantic.Field
- Return type:
- pydantic_xml.computed_attr(prop=None, *, name=None, ns=None, **kwargs)[source]#
Marks a property as an xml attribute.
- pydantic_xml.computed_element(prop=None, *, tag=None, ns=None, nsmap=None, nillable=False, **kwargs)[source]#
Marks a property as an xml element.
- pydantic_xml.create_model(__model_name, *, __tag__=None, __ns__=None, __nsmap__=None, __ns_attrs__=None, __skip_empty__=None, __search_mode__=None, __base__=None, __module__=None, **kwargs)[source]#
Dynamically creates a new pydantic-xml model.
- Parameters:
__model_name (str) – model name
__nsmap__ (Optional[Dict[str, str]]) – element namespace map
__skip_empty__ (Optional[bool]) – skip empty elements (elements without sub-elements, attributes and text)
__search_mode__ (Optional[SearchMode]) – element search mode
__base__ (Optional[Union[Type[Model], Tuple[Type[Model], ...]]]) – model base class
__module__ (Optional[str]) – module name that the model belongs to
kwargs (Any) – pydantic model creation arguments. See https://docs.pydantic.dev/latest/api/base_model/#pydantic.create_model.
- Returns:
created model
- Return type:
Type[Model]