Python XML Parsing Children and grand children attributes
<p>I have been trying this on and off for the last two weeks and going over python's documents for XML parsing. I still can figure out if it's an Xpath thing or not. If anyone can provide some help I would greatly appreciate it.</p>

<p>My XML file has many children and I am using the root.findall() to get the attribute for myAccessPoints and then three children below it I have an element with several attributes I want to extract. However, so far, I'm only able to do this with 2 for loops.</p>

<pre><code>import xml.etree.ElementTree as ET



def apData():

    tree = ET.parse("project.xml")

    root = tree.getroot()
    for topLevels in root.findall("./myAccessPoints//*[@id]"):
        myApId = topLevels.get('id')
        print("AP:%s" % myApId)
        print()
        #return myApId

    for radio in root.findall("./accessPoints/accessPoint/radio/*"):
        rChannel = radio.get('primaryNumber')
        rMac = radio.get('mac')
        rSsid = radio.get('primaryNumber')
        print(rChannel, rMac, rSsid)
        #return rChannel, rMac, rSsid
</code></pre>

<p>Here is an example of the XML file:</p>

<pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;project&gt;
  &lt;maps&gt;
    &lt;map id="0" name="floorplan" pixelsPerMeter="47.808212118953044" type="fspl"/&gt;
  &lt;/maps&gt;
  &lt;accessPoints&gt;
    &lt;accessPoint id="0" userDefinedPosition="false"&gt;
      &lt;radio type="measured"&gt;
        &lt;accessPointMeasurement mac="a0:63:91:21:c4:f8" ssid="Eggs" primaryNumber="7" primaryFrequencyMhz="2442" centerNumber="7" bandwidthMhz="20" security="WPA2" informationElements="000445676773010882840b162430486c0301072a01042f010430140100000fac040100000fac040100000fac020c0032040c1218602d1afc181fffff0000000000000000000000000000000000000000003d16070017000000000000000000000000000000000000004a0e14000a002c01c8001400050019007f0101dd890050f204104a0001101044000102103b00010310470010177b8b3ae292d7c44b93d4616ff30e7e1021000d4e4554474541522c20496e632e1023000a574e44523334303076331024000a574e44523334303076331042000230311054000800060050f20400011011000a574e4452333430307633100800020004103c0001031049000600372a000120dd090010180204f0040000dd180050f2020101800003a4000027a4000042435e0062322f00"&gt;
          &lt;technologies&gt;
            &lt;technology band="802.11g"/&gt;
            &lt;technology band="802.11b"/&gt;
            &lt;technology band="802.11n"/&gt;
          &lt;/technologies&gt;
        &lt;/accessPointMeasurement&gt;
      &lt;/radio&gt;
    &lt;/accessPoint&gt;
    &lt;accessPoint id="1" userDefinedPosition="false"&gt;
</code></pre>

<p>Eventually I would take the access point element attributes like so --></p>

<p>accessPoint id </p>

<p>accessPointMeasurement mac, ssid, primaryNumber</p>

<p>technology band</p>

<p>technology band</p>

<p>technology band</p>

<p>Some accessPoint elements have 2 sets of radios so I would have to get accessPointMeasurement attributes twice.</p>

<p>I imagine I'll have to create a class and within the class I'd have to make lists or dictionaries within themselves.</p>

<p>I'm not asking anyone to do anything for me other than to understand how I can get each access point and it's attributes in one for loop (If it's even possible).</p>

<p>Thanks for your help.</p>