XmlFormatter

class dictIO.formatter.XmlFormatter(*, omit_prefix: bool = True, integrate_attributes: bool = True, remove_node_numbering: bool = True)

Bases: Formatter

Formatter to serialize a dict into a string in xml format.

Defaults:

namespaces: ‘https://www.w3.org/2009/XMLSchema/XMLSchema.xsd’ n root tag: ‘NOTSPECIFIED’ n root attributes: None n indent: 4

Defaults can be overwritten by adding a subdict ‘_xmlOpts’ to dict, containing ‘_nameSpaces’, ‘_rootTag’, ‘_rootAttributes’.

Adding a subdict ‘_attributes’ to a subdict inside dict causes the XmlFormatter to write xml attributes. In contrast to xml, there are some specialties in dict format what need to be customized:

property | in xml | sibling in dict |
:———– | :—————– | :————– |
general name | root tag | the file name is the ‘root tag’ and hence the name of the dict |
name | element name (tag) | sub-dict name (has to be unique) |
| (multiple occurrences allowed due to attributes) | |
attributes | as required | attributes need to be provided in a separate subdict “_attributes” to take action |
style | namespace(s) | style guide, no namespaces (ns can be provided in a subdict “_xmlOpts”) |
__init__(*, omit_prefix: bool = True, integrate_attributes: bool = True, remove_node_numbering: bool = True) None

Define default configuration for XmlFormatter.

Methods

__init__(*[, omit_prefix, ...])

Define default configuration for XmlFormatter.

add_double_quotes(arg)

Add double quotes to a string.

add_single_quotes(arg)

Add single quotes to a string.

format_bool(arg)

Format a boolean.

format_empty_string(arg)

Format an empty string.

format_expression_string(arg)

Format an expression.

format_float(arg)

Format a floating point number.

format_int(arg)

Format an integer.

format_key(arg)

Format a key.

format_multi_word_string(arg)

Format a multi word string.

format_none()

Format None value.

format_reference_string(arg)

Format a reference.

format_single_word_string(arg)

Format a single word string.

format_string(arg)

Format a string.

format_string_with_nested_string(arg)

Format a string that contains a nested string.

format_value(arg)

Format a single value.

format_values(arg)

Format multiple values.

get_formatter([target_file])

Return a Formatter instance matching the type of the target file to be formatted (factory method).

populate_into_element(element, arg[, xsd_uri])

Populate arg into the XML element node.

to_string(arg)

Create a string representation of the passed in dict in XML format.

populate_into_element(element: Element, arg: MutableMapping[Any, Any] | MutableSequence[Any] | Any, xsd_uri: str | None = None) None

Populate arg into the XML element node.

If arg is a dict or list, method will call itself recursively until all nested content within the dict or list is populated into nested elements, eventually creating an XML dom.

Parameters:
  • element (Element) – element which will be populated

  • arg (MutableMapping[Any, Any] | MutableSequence[Any] | Any) – value to be populated into the element

  • xsd_uri (str, optional) – xsd uri, by default None

to_string(arg: MutableMapping[K, V]) str

Create a string representation of the passed in dict in XML format.

Parameters:

arg (MutableMapping[K, V]) – dict to be formatted

Returns:

string representation of the dict in XML format

Return type:

str