XmlFormatter¶
- class dictIO.formatter.XmlFormatter(*, omit_prefix: bool = True, integrate_attributes: bool = True, remove_node_numbering: bool = True)¶
Bases:
FormatterFormatter to serialize a dict into a string in xml format.
The default XML configuration uses the XML Schema namespace, the NOTSPECIFIED root tag, no root attributes, and four-space indentation. Override these defaults with an _xmlOpts subdictionary containing _nameSpaces, _rootTag, or _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”) |- Parameters:
omit_prefix (bool)
integrate_attributes (bool)
remove_node_numbering (bool)
- omit_prefix¶
Whether namespace prefixes are removed from element tags.
- integrate_attributes¶
Whether recognized dictionary attributes are serialized as XML attributes.
- remove_node_numbering¶
Whether generated node numbers are removed from element names.
- __init__(*, omit_prefix: bool = True, integrate_attributes: bool = True, remove_node_numbering: bool = True) None¶
Initialize XML formatting options.
- Parameters:
omit_prefix (bool) – Whether to remove namespace prefixes from serialized element tags. Defaults to True.
integrate_attributes (bool) – Whether to serialize recognized dictionary attributes as XML attributes. Defaults to True.
remove_node_numbering (bool) – Whether to remove generated node numbers from element names. Defaults to True.
- Return type:
None
Methods
__init__(*[, omit_prefix, ...])Initialize XML formatting options.
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: object, 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) – XML element to populate.
arg (object) – Value or nested container to add to the element.
xsd_uri (str | None) – Namespace URI for created child elements. If None, use the namespace configured by the caller. Defaults to None.
- Return type:
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