kak...@gmail.com, 14.05.2010 16:57:
Hi to all, let's say we have the following Xml
<team>
   <player name='Mick Fowler' age='27' height='1.96m'>
     <points>17.1</points>
     <rebounds>6.4</rebounds>
   </player>
   <player name='Ivan Ivanovic' age='29' height='2.04m'>
     <points>15.5</points>
     <rebounds>7.8</rebounds>
   </player>
</team>

How can i get the players name, age and height?

Here's an overly complicated solution, but I thought that an object oriented design would help here.


  import xml.etree.ElementTree as ET

  class Player(object):
     def __init__(self, name, age, height):
         self.name, self.age, self.height = name, age, height

  attributes = ['name', 'age', 'height']

  players = []
  for _, element in ET.iterparse("teamfile.xml"):
      if element.tag == 'player':
          players.append(
              Player(*[ element.get(attr) for attr in attributes ]))

  for player in players:
      print player.name, player.age, player.height


Stefan

--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to