473,473 Members | 1,758 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 6933
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Amol | last post by:
Hi, I want to fetch some values from a XML file, using a java program. I want to retrieve value of path tag for e.g<path>some path</path> tag.(Marked with **** in the below XML file Example) ...
3
by: Miguel J. Jiménez | last post by:
Hi, I have the following node: <node> Some text here with lots of inside it... </node> and I would like it to transfrom it using XSLT to the following: Some text<br/> here with</br> lots...
12
by: Anna | last post by:
Hi all, I posted the same question this afternoon but my message isn't showing up, so I thought I'd give it another try.... in case you should see it later I apologize for posting the same...
21
by: Michael Bierman | last post by:
Please forgive the simplicy of this question. I have the following code which attempts to determine the color of some text and set other text to match that color. It works fine in Firefox, but does...
4
by: ryu | last post by:
Hi all, I have a xml document where I have to replace the value of node type that is text. For example, if the value of a node whose type is 'text' is 'Toyota', I would like it to be...
3
by: gregpinero | last post by:
Hi guys, What I'm trying to do is find all instances of an acronymn such as IBM on a webpage and replace it with <acronym title="International Business Machines">IBM</acronym>. However in my...
3
by: Irocivan | last post by:
Hello, I download a nice collapse menu for free distribution from the internet. It works very well except that none of the parents nodes are clickable (i.e. when clicking on the parent node, the...
3
by: edai | last post by:
I am working on a code where I am loading XML data from a file on the server using ....... if (xmlhttp.readyState==4) { value=xmlhttp.responseText; var parser=new DOMParser();...
1
by: neovantage | last post by:
Hey all, I am using a PHP script which creates headings at run time in a sense at page execution. I am stuck a with a very little problem which i am sure i will have the solution from experts. ...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.