Replace value of node using getElementsByTagName

Ouray Viney oviney at gmail.com
Sun Aug 10 03:37:29 EDT 2008


Xml

<ib>8.4.27.5</ib>

python

from xml.dom import minidom
xmldoc = minidom.parse('C:\TestProfile.xml')
xmldoc

ibNodeList = xmldoc.getElementsByTagName("ib")
firstChild = xmldoc.firstChild

for node in xmldoc.getElementsByTagName('ib'):  # visit every node
<ib>
    print node.toxml()
    node.replaceChild("<ib>8.4.27.5</ib>",node)

Error

Traceback (most recent call last):
  File "C:\Python25\Lib\site-packages\pythonwin\pywin\framework
\scriptutils.py", line 310, in RunScript
    exec codeObject in __main__.__dict__
  File "C:\Documents and Settings\vineyo\Desktop\parseXml.py", line
17, in <module>
    node.firstChild.replaceChild("<ib>8.4.27.5</ib>",node.firstChild)
  File "C:\Python25\lib\xml\dom\minidom.py", line 899, in replaceChild
    self.nodeName + " nodes do not have children")
HierarchyRequestErr: #text nodes do not have children

Question:
Is there an easy way to replace the node value of <ib>?  Perhaps I am
trying to use the wrong python XML library?  Any help would be greatly
appreciated.



More information about the Python-list mailing list