[XML-SIG] How do I replace multiple elements in a document? (using ElementTree)

Tony McDonald tony.mcdonald at ncl.ac.uk
Wed Nov 30 13:50:13 CET 2005

On 29 Nov 2005, at 11:47, Fredrik Lundh wrote:

Apologies for replying a bit late, I was trying to fix things up  
myself and didn't check back with the mailing list until later...

> how about:
> xml = """<?xml version="1.0"?>
> <doc>
> <begin>start</begin>
> <replaceme>with something else</replaceme>
> <end>finish</end>
> </doc>
> """
> root = XML(xml)
> new = Element("new")
> new.text = "Hi"
> new.tail = "\n"
> for index, element in enumerate(root):
>      if element.tag == 'replaceme':
>          root[index] = new
> dump(root)
> this prints
> <doc>
> <begin>start</begin>
> <new>Hi</new>
> <end>finish</end>
> </doc>

* -- blinks -- *

enumerate? you can tell who's not been keeping up with python release  

I had been using the findIndex function that Greg Wilson posted a  
while back (thanks Greg!)

     def findIndex(parent, child):
         for i in range(len(parent)):
             if parent[i] is child:
                 return i
         return -1

But this is more *elegant*

> to do this recursively, you can either put the code in a function
> that calls itself, or you can simply add an extra loop:
> for parent in root.getiterator():
>     for index, element in enumerate(parent):
>         if element.tag == 'replaceme':
>             parent[index] = new
> hope this helps!

Oh it does, it does - many thanks indeed!

BTW (everyone), what's the correct import procedure to get  
cElementTree goodness?

I did have this,

from elementtree.ElementTree import XML, ElementTree, Element,  
SubElement, dump

and went to this;

from cElementTree import XML, ElementTree, Element, SubElement, dump

Which imports fine, but there's something amiss because;
root = ElementTree(file=filename)
elements = root.getiterator()
print "LEN", len(elements)

which gave 634 elements using python ElementTree but gives len() of  
unsized object when using the eElementTree version, and there does  
seem to be something amiss ...

python 2.4.1, OS-X 10.4.3

dir(elements) - cElementTree (version 1.0.2)
['__class__', '__delattr__', '__doc__', '__getattribute__',  
'__hash__', '__init__', '__iter__', '__new__', '__reduce__',  
'__reduce_ex__', '__repr__', '__setattr__', '__str__', 'gi_frame',  
'gi_running', 'next']

dir(elements) - python ElementTree (version 1.2)
['__add__', '__class__', '__contains__', '__delattr__',  
'__delitem__', '__delslice__', '__doc__', '__eq__', '__ge__',  
'__getattribute__', '__getitem__', '__getslice__', '__gt__',  
'__hash__', '__iadd__', '__imul__', '__init__', '__iter__', '__le__',  
'__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__',  
'__reduce_ex__', '__repr__', '__reversed__', '__rmul__',  
'__setattr__', '__setitem__', '__setslice__', '__str__', 'append',  
'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse',  

Which is 'beyond my ken...'

I can stick with python ElementTree if needed, but many thanks for  
any pointers!

Dr Tony McDonald, Asst Director, FMSC. 0191 246 4543
School of Medical Education Development
Project Manager, FDTL-4 ePortfolios http://www.eportfolios.ac.uk/

More information about the XML-SIG mailing list