Replace value of node using getElementsByTagName

O

Ouray Viney

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.
 
K

Ken Starks

Ouray said:
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.


"""
 
S

Stefan Behnel

Ouray said:
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
 
O

Ouray Viney

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
 
S

Stefan Behnel

Ouray said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top