martinblech / xmltodict

Python module that makes working with XML feel like you are working with JSON
MIT License
5.46k stars 465 forks source link

Overwriting elements #332

Closed SvenPVoigt closed 1 year ago

SvenPVoigt commented 1 year ago

I am using version 0.12.0 of xmltodict installed from Anaconda.

Really like how I can set attributes on elements using the @ notation using this library, which isn't possible in the dict2xml or dicttoxml libraries.

Trying to create some graphml data from a dict which has format like:

<node id="n10"/>
<edge source="n0" target="n2"/>

Problem is that the nodes are overwritten by the last node for some reason by the unparse function. For example the dictionary with two nodes:

unparse({
    "root": {
        "node": {"@id": 0},
        "node": {"@id": 1},
    }
}, pretty=True)

Creates the xml with one node:

<?xml version="1.0" encoding="utf-8"?>
<root>
    <node id="1"></node>
</root>

Any parameters I am not setting? Or is there a reason this behavior should be expected?