A doubt on DOM parser used with Python
Posted
by fixxxer
on Stack Overflow
See other posts from Stack Overflow
or by fixxxer
Published on 2010-05-05T16:52:22Z
Indexed on
2010/05/05
16:58 UTC
Read the original article
Hit count: 332
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>
© Stack Overflow or respective owner