Problem inserting an element where I want it using lxml

Alan Meyer ameyer2 at yahoo.com
Wed Jan 5 06:20:22 EST 2011


On 01/05/2011 02:47 AM, Stefan Behnel wrote:

> ...
> Looks trivial to me. ;)
> ...
> ".iter()" gives you a recursive iterator that will also yield the
> "something" Element in your case, thus the incorrect counting. You only
> want the children, so you should iterate over the Element itself.

Thanks Stephan.

I went home and went to sleep and woke up in the middle of the night and 
thought, wait a minute, iter() is giving me a depth first list of 
elements but insert() is indexing children of the parent.

I think I must have been up too late.

> There is an .index() method on Elements that does what you want to
> achieve here. However, the right way to do it is to use ".addnext()".
>
> http://codespeak.net/lxml/api/lxml.etree._Element-class.html
>
> Stefan
>
Those are exactly the functions I wanted.  I didn't see them (and still 
don't) in the Python ElementTree documentation and thought I had to use 
parent.insert().

Thanks again.



More information about the Python-list mailing list