471,599 Members | 1,649 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,599 software developers and data experts.

Replace value of node using getElementsByTagName

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.
Aug 10 '08 #1
4 6756
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.
"""
Aug 10 '08 #2
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
Aug 10 '08 #3
On Aug 10, 9:01*am, Stefan Behnel <stefan...@behnel.dewrote:
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.ElementTre e-class). It is
still unclear to me how I could change the value in the <ibelement.
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
Aug 10 '08 #4
Ouray Viney wrote:
In your example you show:

ib.text = calculate_new_value(ib.text)

I don't know what calculate_new_value() represents.
It's meant as pseudo-code. Just take the function name literally and replace
it by something that gives you the value you want to assign (as you already
did in your code).

Good news, while writing this question I tested out the code and it
worked :).
Happy to see you've found it. Writing down a question is fairly often enough
to actually solve it.

Please excuse my trivial questions, I am new to python and appreciate
your help.
You're welcome.

Stefan
Aug 10 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Miguel J. Jiménez | last post: by
3 posts views Thread by gregpinero | last post: by
reply views Thread by leo001 | last post: by
reply views Thread by Anwar ali | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.