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)[source]#

Serializes the object to an xml tree.

Parameters:

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

Returns:

object xml representation

Return type:

Element

to_xml(*, skip_empty=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)

  • kwargs (Any) – additional xml serialization arguments

Returns:

object xml representation

Return type:

Union[str, bytes]

model_computed_fields: ClassVar[dict[str, ComputedFieldInfo]] = {}#

A dictionary of computed field names and their corresponding ComputedFieldInfo objects.

model_config: ClassVar[ConfigDict] = {}#

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

model_fields: ClassVar[dict[str, FieldInfo]] = {}#

Metadata about the fields defined on the model, mapping of field names to [FieldInfo][pydantic.fields.FieldInfo].

This replaces Model.__fields__ from Pydantic V1.

class pydantic_xml.RootXmlModel(root=PydanticUndefined)[source]#

Bases: RootModel[~RootModelRootType], BaseXmlModel, Generic[RootModelRootType]

Base pydantic-xml root model.

Parameters:

root (RootModelRootType) –

model_computed_fields: ClassVar[dict[str, ComputedFieldInfo]] = {}#

A dictionary of computed field names and their corresponding ComputedFieldInfo objects.

model_config: ClassVar[ConfigDict] = {}#

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

model_fields: ClassVar[dict[str, FieldInfo]] = {'root': FieldInfo(annotation=~RootModelRootType, required=True)}#

Metadata about the fields defined on the model, mapping of field names to [FieldInfo][pydantic.fields.FieldInfo].

This replaces Model.__fields__ from Pydantic V1.

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