Replace value of node using getElementsByTagName
Ken Starks
straton at lampsacos.demon.co.uk
Sun Aug 10 07:23:19 EDT 2008
Ouray Viney wrote:
> 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.
I use 4suite myself.
see section 3.2.1 of the manual:
"""
3.2.1 What about getElementsByTagName()?
The getElementsByTagName() method isn't supported, because there are
better options. In particular, you can just use XPath:
doc.xpath(u"//tagname")
For more possibilities, see getElementsByTagName Alternatives.
"""
More information about the Python-list
mailing list