473,394 Members | 1,173 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 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 6927
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
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...
0
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...

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.