btathalon
btathalon

Reputation: 185

Why is Elementtree iterating over each element even though it isn't a child?

I have created a model that is used to create an object with data gleaned from an xml file using ElementTree to parse the xml file. My project is a couple thousand lines of code but I was able to quickly recreate my problem using the following example.

Sample XML data:

    <data>
       <country name="Liechtenstein">
          <rank>1</rank>
          <year>2008</year>
          <gdppc>141100</gdppc>
          <neighbor name="Austria" direction="E"/>
          <neighbor name="Switzerland" direction="W"/>
       </country>
       <country name="Singapore">
          <rank>4</rank>
          <year>2011</year>
          <gdppc>59900</gdppc>
          <neighbor name="Malaysia" direction="N"/>
       </country>
       <country name="Panama">
          <rank>68</rank>
          <year>2011</year>
          <gdppc>13600</gdppc>
          <neighbor name="Costa Rica" direction="W"/>
          <neighbor name="Colombia" direction="E"/>
       </country>
   </data>

Model:

class neighbor(object):
   name = ""
   direction = ""

class neighborList(object):
   neighbor = []

class country(object):
   name = ""
   rank = ""
   year = ""
   gdppc = ""
   neighborList = neighborList()

class countryList(object):
   country = []

class data(object):
   countryList = countryList()

Parser:

    from xml.etree import ElementTree as ET
    import countries_model as ctry

    def CountriesCrusher(filename):

        xmldoc = ET.parse(filename)
        element = xmldoc.getroot()

        _data = ctry
        _countryList = ctry.countryList()  

        for firstLevel in element.findall('country'):
            b = ctry.country()
            b.rank = firstLevel.find('rank').text
            b.year = firstLevel.find('year').text
            b.gdppc = firstLevel.find('gdppc').text
            b.neighborList = ctry.neighborList()

            for secondLevel in firstLevel.findall('neighbor'):
                c = ctry.neighbor
                c.direction = secondLevel.attrib.get('direction')
                c.name = secondLevel.attrib.get('name')
                b.neighborList.neighbor.append(c)

            _countryList.country.append(b)

        a = ctry.data()
        a.countryList = _countryList
        _data = a
        return _data

    ictry = CountriesCrusher('countries.xml')

Before I run this I would expect that if I look at ictry.countryList.country I would see three entries and that if I look at ictry.countryList.country[0].neighborList.neighbor I would see two neighbor entries for that country but instead I am seeing all five neighbor elements that are in the entire xml file. Why is this happening ??

Upvotes: 1

Views: 44

Answers (1)

Martin Konecny
Martin Konecny

Reputation: 59571

You arent using instance attributes of the class country.

Write your country class (and all others) like so:

class country:
    def __init__(self):
        self.name = ""
        self.rank = ""
        self.year = ""
        self.gdppc = ""
        self.neighborList = neighborList()

Now b = ctry.country() will give you an instance whose attributes will be decoupled/separate from a second call to b = ctry.country(). Your current approach all instances of ctry.country shared the same attributes since they were class attributes, not instance attributes.

Read more about class vs instance attributes here.

Upvotes: 1

Related Questions