minidom

Preserve order of attributes when modifying with minidom

試著忘記壹切 提交于 2019-11-27 04:34:48
问题 Is there a way I can preserve the original order of attributes when processing XML with minidom? Say I have: <color red="255" green="255" blue="233" /> when I modify this with minidom the attributes are rearranged alphabetically blue, green, and red. I'd like to preserve the original order. I am processing the file by looping through the elements returned by elements = doc.getElementsByTagName('color') and then I do assignments like this e.attributes["red"].value = "233" . 回答1: Is there a way

Reading XML using Python minidom and iterating over each node

二次信任 提交于 2019-11-27 01:06:12
问题 I have an XML structure that looks like the following, but on a much larger scale: <root> <conference name='1'> <author> Bob </author> <author> Nigel </author> </conference> <conference name='2'> <author> Alice </author> <author> Mary </author> </conference> </root> For this, I used the following code: dom = parse(filepath) conference=dom.getElementsByTagName('conference') for node in conference: conf_name=node.getAttribute('name') print conf_name alist=node.getElementsByTagName('author') for