Developer Interface#

pydantic xml serialization/deserialization extension

class pydantic_xml.BaseXmlModel[source]#

Bases: BaseModel

Base pydantic-xml model.

classmethod update_forward_refs(**kwargs)[source]#

Try to update ForwardRefs on fields based on this Model, globalns and localns.

Parameters:

kwargs (Any) –

Return type:

None

classmethod from_xml_tree(root)[source]#

Deserializes an xml element tree to an object of cls type.

Parameters:

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

Returns:

deserialized object

Return type:

ModelT

classmethod from_xml(source)[source]#

Deserializes an xml string to an object of cls type.

Parameters:

source (Union[str, bytes]) – xml string

Returns:

deserialized object

Return type:

ModelT

to_xml_tree(*, encoder=None, skip_empty=False)[source]#

Serializes the object to an xml tree.

Parameters:
  • encoder (Optional[XmlEncoder]) – xml type encoder

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

Returns:

object xml representation

Return type:

Element

to_xml(*, encoder=None, skip_empty=False, **kwargs)[source]#

Serializes the object to an xml string.

Parameters:
  • encoder (Optional[XmlEncoder]) – xml type encoder

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

class pydantic_xml.BaseGenericXmlModel[source]#

Bases: BaseXmlModel, GenericModel

Base pydantic-xml generic model.

classmethod from_xml_tree(root)[source]#

Deserializes an xml element tree to an object of cls type.

Parameters:

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

Returns:

deserialized object

Return type:

GenericModelT

class pydantic_xml.XmlEncoder(default=None)[source]#

Bases: object

Xml data encoder.

Parameters:

default (Optional[Callable[[Any], Any]]) –

encode(obj)[source]#

Encodes provided object into a string

Parameters:

obj (Any) – object to be encoded

Returns:

encoded object

Return type:

str

exception pydantic_xml.ModelError[source]#

Bases: BaseError

Model definition error.

class pydantic_xml.XmlAttributeInfo(name=None, ns=None, **kwargs)[source]#

Bases: XmlEntityInfo

Field xml attribute meta-information.

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

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

  • kwargs (Any) – pydantic field arguments. See pydantic.Field

class pydantic_xml.XmlElementInfo(tag=None, ns=None, nsmap=None, **kwargs)[source]#

Bases: XmlEntityInfo

Field xml element meta-information.

Parameters:
class pydantic_xml.XmlWrapperInfo(path, entity=None, ns=None, nsmap=None, **kwargs)[source]#

Bases: XmlEntityInfo

Field xml wrapper meta-information.

Parameters:
  • entity (Optional[XmlEntityInfo]) – wrapped entity

  • path (str) – entity path

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

  • nsmap (Optional[Dict[str, str]]) – element xml namespace map

  • kwargs (Any) – pydantic field arguments

exception pydantic_xml.ParsingError[source]#

Bases: BaseError

Xml parsing error.

pydantic_xml.attr(**kwargs)[source]#

Marks a pydantic field as an xml attribute.

Parameters:

kwargs (Any) – see pydantic_xml.XmlAttributeInfo

Return type:

Any

pydantic_xml.element(**kwargs)[source]#

Marks a pydantic field as an xml element.

Parameters:

kwargs (Any) – see pydantic_xml.XmlElementInfo

Return type:

Any

pydantic_xml.wrapped(*args, **kwargs)[source]#

Marks a pydantic field as a wrapped xml entity.

Parameters:
Return type:

Any