Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Convert an lxml.etree node tree into a dict.
def elem2dict(node):
"""
Convert an lxml.etree node tree into a dict.
"""
d = {}
for e in node.iterchildren():
key = e.tag.split('}')[1] if '}' in e.tag else e.tag
value = e.text if e.text else elem2dict(e)
d[key] = value
return d
@pieterdd
Copy link

pieterdd commented Jan 15, 2019

Note that this implementation doesn't handle newlines and indentation correctly. My version:

def elem2dict(node):
    """
    Convert an lxml.etree node tree into a dict.
    """
    result = {}

    for element in node.iterchildren():
        # Remove namespace prefix
        key = element.tag.split('}')[1] if '}' in element.tag else element.tag

        # Process element as tree element if the inner XML contains non-whitespace content
        if element.text and element.text.strip():
            value = element.text
        else:
            value = elem2dict(element)

        result[key] = value

    return result

@ITJamie
Copy link

ITJamie commented Jul 26, 2019

warning. nether of the above account for xml that might have multiple items with the same key name and thus overwring the the key with new values. patch below

def elem2dict(node):
    """
    Convert an lxml.etree node tree into a dict.
    """
    result = {}

    for element in node.iterchildren():
        # Remove namespace prefix
        key = element.tag.split('}')[1] if '}' in element.tag else element.tag

        # Process element as tree element if the inner XML contains non-whitespace content
        if element.text and element.text.strip():
            value = element.text
        else:
            value = elem2dict(element)
        if key in result:

            
            if type(result[key]) is list:
                result[key].append(value)
            else:
                tempvalue = result[key].copy()
                result[key] = [tempvalue, value]
        else:
            result[key] = value
    return result

@samuelpcabral
Copy link

samuelpcabral commented Oct 4, 2019

warning. nether of the above account for xml that might have multiple items with the same key name and thus overwring the the key with new values. patch below

Very well! it works perfectly.

Thank You

@pjknowles
Copy link

pjknowles commented Nov 3, 2022

Here's a version that introduces also element attributes into the dictionary.

def elem2dict(node, attributes=True):
    """
    Convert an lxml.etree node tree into a dict.
    """
    result = {}
    if attributes:
        for item in node.attrib.items():
            key, result[key] = item

    for element in node.iterchildren():
        # Remove namespace prefix
        key = element.tag.split('}')[1] if '}' in element.tag else element.tag

        # Process element as tree element if the inner XML contains non-whitespace content
        if element.text and element.text.strip():
            value = element.text
        else:
            value = elem2dict(element)
        if key in result:
            if type(result[key]) is list:
                result[key].append(value)
            else:
                result[key] = [result[key], value]
        else:
            result[key] = value
    return result

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment