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.

Parameters:
  • root (Element) – xml element to deserialize the object from

  • context (Optional[Dict[str, Any]]) – pydantic validation context

Returns:

deserialized object

Return type:

ModelT

classmethod from_xml(source, context=None)[source]#

Deserializes an xml string to an object of cls type.

Parameters:
Returns:

deserialized object

Return type:

ModelT

to_xml_tree(*, skip_empty=False, exclude_none=False, exclude_unset=False)[source]#

Serializes the object to an xml tree.

Parameters:
  • skip_empty (bool) – skip empty elements (elements without sub-elements, attributes and text, Nones)

  • exclude_none (bool) – exclude None values

  • exclude_unset (bool) – exclude values that haven’t been explicitly set

Returns:

object xml representation

Return type:

Element

to_xml(*, skip_empty=False, exclude_none=False, exclude_unset=False, **kwargs)[source]#

Serializes the object to an xml string.

Parameters:
  • skip_empty (bool) – skip empty elements (elements without sub-elements, attributes and text, Nones)

  • exclude_none (bool) – exclude None values

  • exclude_unset (bool) – exclude values that haven’t been explicitly set

  • kwargs (Any) – additional xml serialization arguments

Returns:

object xml representation

Return type:

Union[str, bytes]

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].

exception pydantic_xml.ModelError[source]#

Bases: BaseError

Model definition error.

exception pydantic_xml.ParsingError[source]#

Bases: BaseError

Xml parsing error.

pydantic_xml.attr(name=None, ns=None, *, default=PydanticUndefined, default_factory=PydanticUndefined, **kwargs)[source]#

Marks a pydantic field as an xml attribute.

Parameters:
  • name (Optional[str]) – attribute name

  • ns (Optional[str]) – attribute xml namespace

  • 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:

Any

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:
Return type:

Any

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

  • ns (Optional[str]) – element xml namespace

  • 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:

Any

pydantic_xml.computed_attr(prop=None, *, name=None, ns=None, **kwargs)[source]#

Marks a property as an xml attribute.

Parameters:
  • prop (Optional[PropertyT]) – decorated property

  • name (Optional[str]) – attribute name

  • ns (Optional[str]) – attribute xml namespace

  • kwargs (Any) – pydantic computed field arguments. See pydantic.computed_field

Return type:

Union[PropertyT, Callable[[PropertyT], PropertyT]]

pydantic_xml.computed_element(prop=None, *, tag=None, ns=None, nsmap=None, nillable=False, **kwargs)[source]#

Marks a property as an xml element.

Parameters:
Return type:

Union[PropertyT, Callable[[PropertyT], PropertyT]]

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:
Returns:

created model

Return type:

Type[Model]

pydantic_xml.xml_field_serializer(field, /, *fields)[source]#

Marks the method as a field xml serializer.

Parameters:
  • field (str) – field to be serialized

  • fields (str) – fields to be serialized

Return type:

Callable[[SerializerFuncT], SerializerFuncT]

pydantic_xml.xml_field_validator(field, /, *fields)[source]#

Marks the method as a field xml validator.

Parameters:
  • field (str) – field to be validated

  • fields (str) – fields to be validated

Return type:

Callable[[ValidatorFuncT], ValidatorFuncT]