[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

remove element with ElementTree

tdan

3/9/2010 3:36:00 AM

I have been using ElementTree to write an app, and would like to
simply remove an element.
But in ElementTree, you must know both the parent and the child
element to do this.
There is no getparent() function, so I am stuck if I only have an
element.

I am iterating over a table and getting all <td> tags, checking their
text, and conditionally deleting them:

def RemoveElementWithText( topEl, subEl, text ):
for el in topEl.getiterator( subEl ):
if el.text = text:
break
else:
el = None
return el

RemoveElementWithText( table, 'td', 'sometext' )

My table is like so:

<table>
<thead>...
<tbody><tr><td>...

Is there any way to do this in ElementTree? I see lxml.etree does
this nicely, but I want to use python's standard library.
3 Answers

Justin Ezequiel

3/9/2010 9:35:00 AM

0

On Mar 9, 11:35 am, tdan <df.tr...@gmail.com> wrote:
> I have been using ElementTree to write an app, and would like to
> simply remove an element.
> But in ElementTree, you must know both the parent and the child
> element to do this.
> There is no getparent() function, so I am stuck if I only have an
> element.
>

see http://effbot.org/zone/element.htm#accessi...

Stefan Behnel

3/9/2010 3:08:00 PM

0

Justin Ezequiel, 09.03.2010 10:34:
> On Mar 9, 11:35 am, tdan<df.tr...@gmail.com> wrote:
>> I have been using ElementTree to write an app, and would like to
>> simply remove an element.
>> But in ElementTree, you must know both the parent and the child
>> element to do this.
>> There is no getparent() function, so I am stuck if I only have an
>> element.
>
> see http://effbot.org/zone/element.htm#accessi...

Also note that there is an independent ElementTree implementation called
lxml.etree, which has parent pointers.

Stefan

Stefan Behnel

3/9/2010 3:46:00 PM

0

tdan, 09.03.2010 04:35:
> I have been using ElementTree to write an app, and would like to
> simply remove an element.
> But in ElementTree, you must know both the parent and the child
> element to do this.
> There is no getparent() function, so I am stuck if I only have an
> element.
>
> I am iterating over a table and getting all<td> tags, checking their
> text, and conditionally deleting them:
>
> def RemoveElementWithText( topEl, subEl, text ):

Note that all-camel-case names are rather unusual for function names and
rather used for class names. See PEP 8 for a style guide.


> for el in topEl.getiterator( subEl ):
> if el.text = text:
> break
> else:
> el = None
> return el
>
> RemoveElementWithText( table, 'td', 'sometext' )
>
> My table is like so:
>
> <table>
> <thead>...
> <tbody><tr><td>...
>
> Is there any way to do this in ElementTree?

I think the easiest is to iterate not over the elements themselves, but
over their parents, and then to remove all children of the specified tag in
each step.

Stefan