-3

我想使用 xml.dom.minidom 按特定属性对 xml 的某些节点进行排序?任何帮助表示赞赏。由此:

    <Node Name="A" Position = "4"/>
    <Node Name="B" Position = "1"/>
    <Node Name="D" Position = "3"/>
    <Node Name="C" Position = "2"/>

对此:

    <Node Name="B" Position = "1"/>
    <Node Name="C" Position = "2"/>
    <Node Name="D" Position = "3"/>
    <Node Name="A" Position = "4"/>

谢谢

4

1 回答 1

9

Get the nodes and sort them:

nodes = dom.getElementsByTagName('Node')
nodes.sort(key=lambda x: int(x.attributes['Position'].value))

Then write your nodes back to the file.

于 2013-07-01T10:16:59.107 回答