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

Gabriel Farrell gsf at panix.com
Thu Nov 9 22:42:00 CET 2006


On Fri, Nov 10, 2006 at 10:23:49AM +1300, John Fouhy wrote:
> On 10/11/06, Asrarahmed Kadri <ajkadri at googlemail.com> wrote:
> > 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?
> 
> Here's some code to get you started:
> 
> >>> from elementtree import ElementTree
> >>> tree = ElementTree.parse('sample.xml')
> 
> # Make a list of all <company> elements.
> >>> companies = tree.findall('company')
> 
> # Make a dict, mapping the name attrib of <company> elements to the
> elements themselves.
> >>> companiesByName = dict((c.get('name'), c) for c in companies)
> 
> # Access the founder element in the ndtv company.
> >>> ndtvFounder = companiesByName['ndtv'].find('founder')
> 

Oops, I guess John's email and mine zipped by each other on the wires.
I like his use of a dictionary to simplify things.  Funny that we had
nearly the same comment about the <founder> element!

> I notice that sometimes, your <founder> element contains text, and
> sometimes it contains other elements (<one> and <two>).  This will
> make extracting founder information a bit fiddly.. You might want to
> consider changing the structure -- perhaps:
> 
> <company name='foo'>
>  <founders>
>   <founder>Bill Gates</founder>
>   <founder>Steve Jobs</founder>
>   <founder>Larry Wall</founder>
>  </founders>
> </company>
> 
> That way, you could do:
> 
> >>> founderElements = companiesByName['ndtv'].findall('founder')
> >>> founders = [f.text for f in founderElements]
> 
> and that would give you the founder names in a list, regardless of
> whether you had one or many.
> 
> HTH!
> 
> -- 
> John.
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
> 


More information about the Tutor mailing list