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

Tony McDonald tony.mcdonald at ncl.ac.uk
Thu Dec 1 09:45:43 CET 2005


On 30 Nov 2005, at 16:27, Fredrik Lundh wrote:

> Tony McDonald wrote:
>
>> 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 ...
>
> the getiterator method is defined to return "something that you can
> iterate over", which is not necessarily a sequence object.
>
> in ElementTree, it happens to be a list (at least in the 1.2  
> series), but
> cElementTree returns a generator instead.
>
> no matter what version you're using, this
>
>     for elem in root.getiterator():
>         ...
>
> is guaranteed to work.

and indeed it does. Rapidly!


> if you want a list, you can always use the list
> constructor. e.g.
>
>     print "LEN", len(list(root.getiterator()))
>
> </F>
>

Many thanks Fredrik, this thread has helped me out no end!

cheers
Tone
-- 
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