pyguymer3.elem2dict

pyguymer3.elem2dict(elem, /, *, debug=True, simplify=False)[source]

Convert a XML element into a Python dictionary

This function accepts a XML element and recursively turns it in to a Python dictionary.

Parameters:
  • elem (lxml.etree._Element) – the XML element

  • debug (bool, optional) – print debug messages

  • simplify (bool, optional) – Simplify XML elements. If true, then XML elements which only contain text (i.e., no attributes and no children) are collapsed to be simply key = “value” rather than key = { “$$TEXT$$” : “value” }.

Returns:

ans – the XML element as a Python dictionary

Return type:

dict

Notes

If present, non-empty text will be mapped to a “$$TEXT$$” dictionary field (it is assumed that the XML does not contain any tags called “$$TEXT$$” otherwise).

If present, non-empty attributes will be mapped to “::KEY::” dictionary fields (it is assumed that the XML does not contain any tags called “::KEY::” otherwise).

If present, child elements will be mapped to “KEY” dictionary fields. If multiple child elements have identical tag names then the “KEY” dictionary field will be a list of them.

Copyright 2017 Thomas Guymer [1]

References