The pyedb.xml_parser.xml_utils library#

Summary#

parse_xml_to_dict

Parse an XML string into a dictionary.

dict_to_xml

Serialize a dictionary with a single root key into an XML string.

Description#

Minimal stdlib XML <-> dict conversion helpers.

The dictionary convention mirrors the one used by xmltodict:

  • attributes are stored with an @ prefix,

  • text of elements that also have attributes/children is stored under #text,

  • elements with only text are plain strings, empty elements are None,

  • repeated sibling tags become lists.

Namespace prefixes (for example c:Control) are preserved verbatim.

Module detail#

xml_utils.parse_xml_to_dict(xml_string: str) → dict#

Parse an XML string into a dictionary.

Parameters:
xml_stringstr

XML document content.

Returns:
dict

Dictionary with a single key (the root tag).

xml_utils.dict_to_xml(data: dict, pretty: bool = True) → str#

Serialize a dictionary with a single root key into an XML string.

Parameters:
datadict

Dictionary with exactly one root key, using the @/#text convention.

prettybool, optional

Whether to indent the output with tabs. The default is True.

Returns:
str

XML document, including the XML declaration.

Raises:
ValueError

If the data doesn’t have exactly one root element or contains invalid XML names.

xml_utils.escape#
xml_utils.quoteattr#