Python读写XML后保持节点属性顺序不变

Python读写XML后保持节点属性顺序不变

试过xml.etree.ElementTree和xml.dom.minidom两个python的库,发现读取后输出都会改变xml里面节点属性的顺序. 虽然这个顺序其实没什么意义但是有些时候时候会比较纠结,找了好多资料最后在stackoverflow中找到一些有用的资料.最后亲测可用.

原资料地址

python环境:2.7

库:import xml.dom.minidom

  • 修改minidom源码,引入相应的库
from collections import OrderedDict
  • 根据下面代码 注释掉相应的源码 新增 self._attrs = OrderedDict()
__init__(...)
    self._attrs = OrderedDict()
    #self._attrs = {}
writexml(...)
    #a_names.sort()
原文地址:https://www.cnblogs.com/crazyzero/p/10111364.html