开发者

A question about DOM parser used with Python

I'm using the following python code to search for a node in an XML file and changing the value of an attribute of one of it's children.Changes are happening correctly when the node is displayed using toxml().But, when it is written to a file, the attributes rearrange themselves(as seen in the Source and the Final XML below). Could anyone explain how and why this happen? Python code:

#!/usr/开发者_运维知识库bin/env python
import xml
from xml.dom.minidom import parse
dom=parse("max.xml")

#print "Please enter the store name:"
for sku in dom.getElementsByTagName("node"):
    if sku.getAttribute("name") == "store":
        sku.childNodes[1].childNodes[5].setAttribute("value","Delhi,India")
        print sku.toxml()
xml.dom.ext.PrettyPrint(dom, open("new.xml", "w"))

a part of the Source XML:

<node name='store' node_id='515' module='mpx.lib.node.simple_value.SimpleValue'  config_builder=''  inherant='false' description='Configurable Value'>
          <match>
            <property name='1' value='point'/>
            <property name='2' value='0'/>
            <property name='val' value='Store# 09204 Staten Island, NY'/>
            <property name='3' value='str'/>
          </match>
        </node>

Final XML :

<node config_builder="" description="Configurable Value" inherant="false" module="mpx.lib.node.simple_value.SimpleValue" name="store" node_id="515">
              <match>
                <property name="1" value="point"/>
                <property name="2" value="0"/>
                <property name="val" value="Delhi,India"/>
                <property name="3" value="str"/>
              </match>
            </node>


Per XML's standards for the DOM, attributes are not held as an ordered collection; in Python's xml.dom implementations, they're a NamedNodeMap, whose docs say:

The order you get the attributes in is arbitrary but will be consistent for the life of a DOM

In particular, there's no promise that this arbitrary order will be the same as the (semantically irrelevant) order found in the XML source that was parsed to build this DOM.


There is no guarantee on the ordering of sub-elements or attibutes in the XML spec. you should NOT rely on ordering of attributes or sub-elements in your business logic, it is guaranteed to not work as expected with all the various parsers. As a side note, I think ElementTree is a much better way to manipulate the DOM than minidom, especially if you are using 2.5.x or newer it is built-in.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜