Open mathijssh opened 1 year ago
If the item would contain the parent's (root) attributes as keys, then this would break if any node have the same name as a parent's attribute.
import xmltodict
xml = """
<root_element>
<node id="1"><id x="foo"/></node>
<node id="2"><id x="bar"/></node>
</root_element>
""".strip()
def handle_item(path, item):
print("handle_item", path, "/", item)
return True
xmltodict.parse(xml, item_depth=2, item_callback=handle_item)
Output:
handle_item [('root_element', None), ('node', {'id': '1'})] / {'id': {'@x': 'foo'}}
handle_item [('root_element', None), ('node', {'id': '2'})] / {'id': {'@x': 'bar'}}
Let's look at the first print handle_item [('root_element', None), ('node', {'id': '1'})] / {'id': {'@x': 'foo'}}
Should {'id': {'@x': 'foo'}}
be {'id': 1}
instead?
Accessing the last item in the root tuple should be easy enough and it will not break things.
Thank you, indeed makes sense to prevent such a collision! And as long as the data of the attributes remains available to the user via the path
tuple then it's great. Would it be an idea to point users to this in the docs?
Thanks again for the swift and clear reponse!
I'm currently parsing an XML document in the format of
stream-parsing it in doc-prescribed format of
I'm interested in all the
node
s' data, including their attributes. When parsing this withxmltodict.parse(item_depth=2,item_callback=handle_event ...)
I could not find theid
attribute in theelement
dict. After some "debugging" I found that attribute hidden in theevent
tuple. Is this intended/desired behavior? I personally would prefer to have those attributes as keys in theelement
dict. Curious to hear any thoughts :)