问题描述
有没有办法在使用 minidom 处理 XML 时保留属性的原始顺序?
Is there a way I can preserve the original order of attributes when processing XML with minidom?
假设我有:<color red="255" green="255" blue="233"/>
当我用 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.
我通过循环处理由 elements = doc.getElementsByTagName('color')
返回的元素来处理文件,然后我做这样的分配 e.attributes["red"].value = "233"
.
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"
.
推荐答案
使用 minidom no,用于存储属性的数据类型是无序字典.pxdom 可以做到,但速度要慢得多.
With minidom no, the datatype used to store attributes is an unordered dictionary. pxdom can do it, though it is considerably slower.
这篇关于使用 minidom 修改时保留属性顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!