Using XML ElementTree to create list of objects with atrributes
<p>I use the python requests module to get XML from the TeamCity rest api that looks like this:</p>

<pre><code>&lt;triggers count="10"&gt;
&lt;trigger id="TRIGGER_1240" type="buildDependencyTrigger"&gt;
    &lt;properties count="2"&gt;
        &lt;property name="afterSuccessfulBuildOnly" value="true"/&gt;
        &lt;property name="dependsOn" value="bt191"/&gt;
    &lt;/properties&gt;
&lt;/trigger&gt;
&lt;trigger id="TRIGGER_1241" type="buildDependencyTrigger"&gt;
    &lt;properties count="2"&gt;
        &lt;property name="afterSuccessfulBuildOnly" value="true"/&gt;
        &lt;property name="dependsOn" value="bt171"/&gt;
    &lt;/properties&gt;
&lt;/trigger&gt;
&lt;trigger id="TRIGGER_1242" type="buildDependencyTrigger"&gt;
    &lt;properties count="2"&gt;
        &lt;property name="afterSuccessfulBuildOnly" value="true"/&gt;
        &lt;property name="dependsOn" value="bt167"/&gt;
    &lt;/properties&gt;
&lt;/trigger&gt;
&lt;trigger id="TRIGGER_1243" type="buildDependencyTrigger"&gt;
    &lt;properties count="2"&gt;
        &lt;property name="afterSuccessfulBuildOnly" value="true"/&gt;
        &lt;property name="dependsOn" value="bt164"/&gt;
    &lt;/properties&gt;
&lt;/trigger&gt;
&lt;trigger id="TRIGGER_1244" type="buildDependencyTrigger"&gt;
    &lt;properties count="2"&gt;
        &lt;property name="afterSuccessfulBuildOnly" value="true"/&gt;
        &lt;property name="dependsOn" value="bt364"/&gt;
    &lt;/properties&gt;
&lt;/trigger&gt;
&lt;trigger id="TRIGGER_736" type="buildDependencyTrigger"&gt;
    &lt;properties count="2"&gt;
        &lt;property name="afterSuccessfulBuildOnly" value="true"/&gt;
        &lt;property name="dependsOn" value="Components_Ratchetdb"/&gt;
    &lt;/properties&gt;
&lt;/trigger&gt;
&lt;trigger id="TRIGGER_149" type="buildDependencyTrigger"&gt;
    &lt;properties count="2"&gt;
        &lt;property name="afterSuccessfulBuildOnly" value="true"/&gt;
        &lt;property name="dependsOn" value="Components_Filedb"/&gt;
    &lt;/properties&gt;
&lt;/trigger&gt;
&lt;trigger id="TRIGGER_150" type="buildDependencyTrigger"&gt;
    &lt;properties count="2"&gt;
        &lt;property name="afterSuccessfulBuildOnly" value="true"/&gt;
        &lt;property name="dependsOn" value="bt168"/&gt;
    &lt;/properties&gt;
&lt;/trigger&gt;
&lt;trigger id="TRIGGER_1130" type="buildDependencyTrigger"&gt;
    &lt;properties count="2"&gt;
        &lt;property name="afterSuccessfulBuildOnly" value="true"/&gt;
        &lt;property name="dependsOn" value="Components_Tbldb"/&gt;
    &lt;/properties&gt;
&lt;/trigger&gt;
&lt;trigger id="vcsTrigger" type="vcsTrigger" inherited="true"&gt;
    &lt;properties count="3"&gt;
        &lt;property name="quietPeriod" value="60"/&gt;
        &lt;property name="quietPeriodMode" value="USE_DEFAULT"/&gt;
        &lt;property name="triggerRules" value="-:version.properties&amp;#xA;-:comment=^Incremented:**&amp;#xA;-:**/*-schema.sql"/&gt;
    &lt;/properties&gt;
&lt;/trigger&gt;
</code></pre>

<p></p>

<p>I am trying to create a list of "trigger" objects using a class. Ideally the object would have id, type, and a list of properties attributes as dictionaries of {name : value}.  My code so far is:</p>

<pre><code>class triggerList:
     def __init__(self, triggerId, triggerType):
         self.id = triggerId
         self.type = triggerType
         self.properties = []

     def add_property(self, buildProperty):
         self.properties.append(buildProperty)

def getAllTriggers(buildId):
     url = path + 'buildTypes/id:' + buildId + '/triggers'
     r = requests.get(url, auth=auth)
     tree = ElementTree.fromstring(r.content)
     listOfTriggers = []

     for trigger in tree.iter('trigger'):
         triggerType = trigger.get('type')
         triggerId = trigger.get('id')
         triggerName = str(triggerId)
         triggerName = triggerList(triggerId, triggerType)
         listOfTriggers.append(triggerName)

         for triggerProperty in tree.iter('property'):
             propertyName = triggerProperty.get('name')
             propertyValue = triggerProperty.get('value')
             propDict = {propertyName : propertyValue}
             triggerName.add_property(propDict)
</code></pre>

<p>This gives me a list of objects but every object has a list of every property dictionary.  This is the output:</p>

<pre><code>a = listOfTriggers[1]
print a.id, a.type, a.properties

&gt;&gt;&gt;  TRIGGER_1241 buildDependencyTrigger [{'afterSuccessfulBuildOnly': 'true'}, {'dependsOn': 'bt191'}, {'afterSuccessfulBuildOnly': 'true'}, {'dependsOn': 'bt171'}, {'afterSuccessfulBuildOnly': 'true'}, {'dependsOn': 'bt167'}, {'afterSuccessfulBuildOnly': 'true'}, {'dependsOn': 'bt164'}, {'afterSuccessfulBuildOnly': 'true'}, {'dependsOn': 'bt364'}, {'afterSuccessfulBuildOnly': 'true'}, {'dependsOn': 'Components_Ratchetdb'}, {'afterSuccessfulBuildOnly': 'true'}, {'dependsOn': 'Components_Filedb'}, {'afterSuccessfulBuildOnly': 'true'}, {'dependsOn': 'bt168'}, {'afterSuccessfulBuildOnly': 'true'}, {'dependsOn': 'Components_Tbldb'}, {'quietPeriod': '60'}, {'quietPeriodMode': 'USE_DEFAULT'}, {'triggerRules': '-:version.properties\n-:comment=^Incremented:**\n-:**/*-schema.sql'}]
</code></pre>

<p>I don't know how to stop the loop for just the properties for a specific trigger.  Is there a way to use ElementTree to only get the properties for a specific trigger?  Is there a more efficient way to create this object? </p>

<p>Not directly answering the question, but you may be reinventing the wheel here, check <a href="http://lxml.de/objectify.html" rel="nofollow"><code>lxml.objectify</code> package</a>:</p>

<blockquote>
  <p>The main idea is to hide the usage of XML behind normal Python
  objects, sometimes referred to as data-binding. It allows you to use
  XML as if you were dealing with a normal Python object hierarchy.
  Accessing the children of an XML element deploys object attribute
  access. If there are multiple children with the same name, slicing and
  indexing can be used. Python data types are extracted from XML content
  automatically and made available to the normal Python operators.</p>
</blockquote>

python xml class object elementtree