[Tutor] ElementTree: finding a tag with specific attribute
Bernard Lebel
3dbernard at gmail.com
Fri Sep 16 21:52:39 CEST 2005
Thanks Kent, I'll check your suggestion out.
I have sent you the xml file.
Bernard
On 9/16/05, Kent Johnson <kent37 at tds.net> wrote:
> 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
>
> _______________________________________________
> Tutor maillist - Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>
More information about the Tutor
mailing list