I am able to create xml with duplicate key and follow suggested answer
python dicttoxml same key multiple times
But it hit badly performance issue when i user custom class fore more than 1 million record.
class Node():
"""
creating class to convert dictionary to xml
Returns:
Node : object which contain child tag name
"""
__slots__ = ['_name']
def __init__(self, name):
self._name = name
def __str__(self):
return self._name
def __hash__(self):
return hash(self._name)
I have tried to update node class(above) but did not get efficient time improvement.
Later I move to follow link which suggest to use default which is really faster and data process in less than 5 min
Why is dictionary lookup in Python always slower when using user defined objects as keys?
{ 'data' : { 'check' : {'error' : ['data1', 'data2'] } } }
<data>
<check>
<error>
<item> data1 </item>
<item> data2 </item>
</error>
</check>
</data>
But I am looking xml following way. is there anyway to get following xml.
<data>
<check>
<error> data1 </error>
<error> data2 </error>
</check>
</data>
Node class dict
{'data' : {Node('check') : { Node('error') : "data1", Node('error') : "data2" } } }
with open(fpath, "w") as fxml:
vio_xml_data = dicttoxml(vio_data)
dom = parseString(vio_xml_data)
fxml.writelines(dom.toprettyxml())
CodePudding user response:
This returns exactly what your looking for
from dict2xml import dict2xml
data = {'data':
{'check':
{'error': ["data1", "data2"]}
}
}
xml = dict2xml(data)
print(xml)
OUTPUT
<data>
<check>
<error>data1</error>
<error>data2</error>
</check>
</data>
NOTE: I'm not using your node class here. Just make sure the "error" key in the dict is a list of values.