Skip to content

Instantly share code, notes, and snippets.

@trungdq88
Forked from jacobian/elem2dict.py
Last active December 2, 2015 03:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save trungdq88/6685f22d4c56bed04cb3 to your computer and use it in GitHub Desktop.
Save trungdq88/6685f22d4c56bed04cb3 to your computer and use it in GitHub Desktop.
Convert an lxml.etree node tree into an object.
def elem2dict(node):
"""
Convert an lxml.etree node tree into an object.
Notice: Since the xml and object (json) structure is not the same,
this utils will not work correctly if there is an xml element that
contains multiple duplicate child elements. For example:
<root>
<name>Hello</name>
<name>Is it me</name>
<hello>You looking for</hello>
</root>
There are 2 tags <name> and one tag <hello>, therefore this utils
do not know if it should convert <root> to an object or an array,
it will return a wrong value.
{
root: [
"Hello",
"Is it me",
"You looking for"
]
}
@see: https://gist.github.com/trungdq88/6685f22d4c56bed04cb3
:param node:
"""
d = {}
for e in node.iterchildren():
key = e.tag.split('}')[1] if '}' in e.tag else e.tag
value = e.text if len(e) == 0 else elem2dict(e)
if isinstance(d, list):
d.append(value)
else:
if key in d:
d = [d[key], value]
else:
d[key] = value
return d
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment