[Tutor] Help with Elementtree ...how to access the attributes..

Gabriel Farrell gsf at panix.com
Thu Nov 9 22:27:29 CET 2006

On Thu, Nov 09, 2006 at 06:23:50PM +0000, Asrarahmed Kadri wrote:
> Hi Folks,
> I am trying to parse XML documents using elementtree.
> I have just started with it. Can somebody help me with how to select nodes
> with a particular atribute set to some value. For example, I have the
> following file. Now I want to access the founder element of the company
> whose attribute is set to 'ndtv'. Can somebody help me with this?

with elementtree:

    from elementtree import ElementTree as et

    tree = et.parse('temp.xml')
    company_list = tree.findall('company')
    for company in company_list:
        if company.attrib['name'] == 'ndtv':
            for founder in company.findall('founder'):
                for child in founder.getchildren():
                    print child.tag, child.text

with lxml (note the difference a full xpath implementation makes):

    from lxml import etree

    tree = etree.parse('temp.xml')
    ndtv_founder_list = tree.xpath('company[@name="ndtv"]/founder')
    for founder in ndtv_founder_list:
        for child in founder.getchildren():
            print child.tag, child.text

I'll leave the modifying of tags and the writing of new xml files up
to you.  A note on your sample.xml: why is it

      <one>larry page</one>
      <two>sergey brin</two>

when it could just be

    <founder>larry page</founder>
    <founder>sergey brin</founder>

?  That would make it easier to parse the text out.  (Probably easier
to produce as well.)


> sample.xml
> <info>
> <company name = 'google'>
> <founder> <one> larry page </one>
> <two> sergey brin</two>
> </founder>
> <focus> search engine </focus>
> </company>
> <company name = 'ndtv'>
> <founder> <one>Pranoy Roy </one>
> <two> Radhika Roy</two>
> </founder>
> <focus> news </focus>
> </company>
> <company  name = "wipro">
> <founder> Azeem Premje </founder>
> <focus> IT services </focus>
> </company>
> </info>
> -- 
> To HIM you shall return.

> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor

More information about the Tutor mailing list