Replace value of node using getElementsByTagName
Ouray Viney
oviney at gmail.com
Sun Aug 10 12:16:48 EDT 2008
On Aug 10, 9:01 am, Stefan Behnel <stefan... at behnel.de> wrote:
> Ouray Viney wrote:
> > Is there an easy way to replace the node value of <ib>? Perhaps I am
> > trying to use the wrong python XML library?
>
> Looks like it. Try ElementTree.
>
> from xml.etree import ElementTree
> tree = ElementTree.parse("yourfile.xml")
> for ib in tree.findall("//ib"):
> ib.text = calculate_new_value(ib.text)
>
> Stefan
Hi:
Thank you all for your responses. I was looking at ElementTree
(http://effbot.org/zone/pythondoc-elementtree-
ElementTree.htm#elementtree.ElementTree.ElementTree-class). It is
still unclear to me how I could change the value in the <ib> element.
I reviewed the availed methods and didn't find anything suitable.
In your example you show:
ib.text = calculate_new_value(ib.text)
I don't know what calculate_new_value() represents.
What I am looking for is the ability to do something like this:
from xml.etree import ElementTree
tree = ElementTree.parse("C:\test.xml")
for ib in tree.findall("//ib"):
ib.text = "somethingnew"
I am also guessing I will need to write the new changes to the file.
tree.write("C:\text.xml")
Good news, while writing this question I tested out the code and it
worked :).
Please excuse my trivial questions, I am new to python and appreciate
your help.
Kind Rgds
More information about the Python-list
mailing list