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

John Fouhy john at fouhy.net
Thu Nov 9 22:23:49 CET 2006

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')

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'>
  <founder>Bill Gates</founder>
  <founder>Steve Jobs</founder>
  <founder>Larry Wall</founder>

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.



More information about the Tutor mailing list