[Tutor] XML: changing value of elements

Stefan Behnel stefan_ml at behnel.de
Thu Jul 16 11:23:25 CEST 2009


Hi,

a new question usually merits a new thread. Especially after a longer time,
replies to older threads tend to remain unread as people simply don't
scroll down far enough to notice them.

You were lucky. :)


Johan Geldenhuys wrote:
> I have another question about writing the xml tree to a file.
> 
> Now, I have parsed and changed my xml tree, but I want to create the same
> tree multiple times with different values and write it to one file.
> 
> 
> Let's use this code:
> 
> 	import xml.etree.ElementTree as ET
> 	doc = ET.parse('signal1.xml')
> 	signal_node = doc.getroot()[0]
> 	signal_node.set('name', 'Test_Name')
> 
> I have this in a function and it returns the signal_node everytime I call
> it, but with a different values. 
> 
> So, How do I take the returned xml and write that to one xml file?

I'm not sure what you mean here. Do you mean: "multiple XML trees into a
single file"? Or each in its own file?

BTW, you don't need to re-parse the tree if all you want is to modify it
and write it back out. You can either keep changing the tree and write it
out multiple times, or deep-copy it (or parts of it) and start over for
each step. Although I guess that parsing might be faster than deep-copying,
especially for ElementTree.

Stefan



More information about the Tutor mailing list