[Tutor] ElementTree: finding a tag with specific attribute
Kent Johnson
kent37 at tds.net
Fri Sep 16 21:44:42 CEST 2005
Bernard Lebel wrote:
> Hello,
>
> With ElementTree, can you search a tag under an Element by not only
> specifying the tag label, but also some tag attribute values? That
> would be in the case where I have several tags with the same label but
> with various attribute values.
>
> Right now I do a loop over every tag of the same label, then test the
> attributes:
>
> for oTag in oElement.findall( 'taglabel' ):
> if oTag.attrib[ 'value' ] == 'xx': do something
I think that is the best you can do. Or switch to something with full XPath support.
FLASH: I just found PDIS XPath which evaluates XPath expressions against ElementTree trees!
http://pdis.hiit.fi/pdis/download/
> I'm looking for something a bit like BeautifulSoup, like:
>
> oTag = oElement.find( 'taglabel', { 'value' : 'xx' } )
>
>
> Btw in case you wonder, I don't use BeautifulSoup because somehow it
> takes 20-30 seconds to parse a 2000-line xml file, and I don't know
> why. ElementTree is proving very performing.
Would you send me privately a copy of your file and your code that reads it with BS? I'm curious why this takes so long.
Kent
More information about the Tutor
mailing list