How to get following sibling of an etree._Element

pypat picture pypat · Aug 1, 2013 · Viewed 7.6k times · Source

I am iterating over elements which I find in a xml file like so:

for elem in fileasxml.xpath("//elementname[some condition]"):
    do something

Now instead of do something I'd like to write the element I have found plus the first following-sibling to a file. Now I know how to write to a file, what I don't know is how to get the following sibling.

I've seen the getnext() method but it doesn't work since the element I'd like to get is not in elem.

Same reason I can't use elem.xpath()

Any ideas ?

Answer

pypat picture pypat · Aug 1, 2013

Okay I was wrong. I made a mistake adding the element I found to a new structure before finding the following sibling.

getnext() actually works !