Re: [xml] Rigth way to recursively delete nodes (python/libxml2)



On Thu, Oct 21, 2004 at 01:55:14AM +0200, Terje Elde wrote:
So far I've come up with something as simple as:

items = mymenu.xpathEval( '//item' )
for i in items:
      if not i.prop('visible'):
              i.unlinkNode()
              i.freeNode()

This would work fine for some situations, but can easily end up trying to free
a node already freed with one of it's parents.

  yes it's easy to get stuck with this. I would say that your
XML structure is really bizarre, having the same node names at various
level looks a bit strange to me. But a relatively simple workaround
is to make sure that you always delete child first before parents.
Since //item returns nodes in document order, simply inverting
the resulting items list before going though it should solve it. You
can also make an XPath query to gather nodes in reverse order, this
is left as an exercise to the reader !

Daniel

-- 
Daniel Veillard      | Red Hat Desktop team http://redhat.com/
veillard redhat com  | libxml GNOME XML XSLT toolkit  http://xmlsoft.org/
http://veillard.com/ | Rpmfind RPM search engine http://rpmfind.net/



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]