-
-
Save trungdq88/6685f22d4c56bed04cb3 to your computer and use it in GitHub Desktop.
Convert an lxml.etree node tree into an object.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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