xsdata.formats.dataclass.serializers.xml module

class xsdata.formats.dataclass.serializers.xml.XmlSerializer(xml_declaration=True, encoding='UTF-8', pretty_print=False, context=<factory>)[source]

Bases: xsdata.formats.bindings.AbstractSerializer

Xml serialize for dataclasses.

Parameters
  • xml_declaration (bool) – Add xml declaration.

  • encoding (str) – Result text encoding.

  • pretty_print (bool) – Enable pretty output.

  • context (XmlContext) – XmlContext instance.

xml_declaration: bool = True
encoding: str = 'UTF-8'
pretty_print: bool = False
context: XmlContext
render(obj, namespaces=None)[source]

Convert the given object tree to xml string.

Optionally provide a namespaces instance with a predefined list of namespace uris and prefixes.

Parameters
Return type

str

render_tree(obj, namespaces=None)[source]

Convert a dataclass instance to a nested Element structure.

Optionally provide a namespaces instance with a predefined list of namespace uris and prefixes.

Parameters
Return type

Element

render_node(parent, obj, namespaces)[source]

Recursively traverse the given object and build the xml tree.

Parameters
render_complex_node(parent, obj, namespaces)[source]

Iterate over the dataclass fields and values and create the element tree.

Parameters
render_sub_nodes(parent, values, var, namespaces)[source]

Iterate of a list of values to render the children of the given parent element.

Parameters
render_sub_node(parent, value, var, namespaces)[source]

Render a child element or text content for the given parent.

Parameters
render_element_node(parent, value, var, namespaces)[source]

Render a child element for the given parent according to the field xml metadata.

Parameters
render_wildcard_node(parent, value, var, namespaces)[source]

Render a child element for the given parent according to the wildcard field metadata.

Parameters
set_xsi_type(parent, value, var, namespaces)[source]

Set the element’s xsi:type if the given value is a derived instance.

Parameters
classmethod next_value(meta, obj)[source]

Return the fields and their values in the correct order according to their definition and sequential metadata.

Sequential fields need to be rendered together in parallel order eg: <a1/><a2/><a1/><a/2></a1>

Parameters
Return type

Iterator[Tuple[XmlVar, Any]]