Reputation: 647
I'm new to python and XML so apologies.
In the XML file I am querying, one of the 'group' categories is called 'Access Points'.
<group id="1">Access Points</group>
How can I filter that, and only output that to the screen?
for i in tree.iter('ap'):
id=i.attrib
if i.find('group')!=None:
group=i.find('group').text
else: group=" "
if i.find('lan_ip')!=None:
ip=i.find('lan_ip').text
else: ip=" "
if i.find('lan_mac')!=None:
mac=i.find('lan_mac').text
else: mac=" "
if i.find('model')!=None:
model=i.find('model').text
else: model=" "
if i.find('name')!=None:
name=i.find('name').text
else: name=" "
if i.find('operating_mode')!=None:
op_mode=i.find('operating_mode').text
else: op_mode=" "
print "\n"+"*"*30
print "#### Details of AP :{}".format(id).ljust(30)
print "*"*30
print "#### Group :{}".format(group).ljust(30)
print "#### AP Name :{}".format(name).ljust(30)
print "#### MAC Address :{}".format(mac).ljust(30)
print "#### IP Address :{}".format(ip).ljust(30)
print "#### Model :{}".format(model).ljust(30)
print "#### Operating Mode:{}".format(op_mode).ljust(30)
print "*" * 30
TEST DATA
<?xml version="1.0" encoding="UTF-8"?>
<amp:amp_ap_list xmlns:amp="http://www.airwave.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1" xsi:schemaLocation="http://www.airwave.com amp_ap_list.xsd">
<ap id="79">
<altitude>0</altitude>
<device_category>fat_ap</device_category>
<client_count>1</client_count>
<firmware>3.2.0</firmware>
<fqdn />
<group id="1">Access Points</group>
<is_up>true</is_up>
<is_remote_ap>true</is_remote_ap>
<remote_outer_ip>10.1.1.1</remote_outer_ip>
<remote_lan_ip>20.20.20.20</remote_lan_ip>
<lan_ip>10.51.1.191</lan_ip>
<lan_mac>00:20:A6:55:E6:E1</lan_mac>
<last_contacted>1350379063</last_contacted>
<last_reboot>1343644897</last_reboot>
<mfgr>Proxim</mfgr>
<model id="44">AP-700</model>
<monitor_only>true</monitor_only>
<name>ORiNOCO-AP-700-55-e6-e1</name>
<operating_mode>ap</operating_mode>
<planned_maintenance_mode>false</planned_maintenance_mode>
<radio index="1">
<antenna />
<antenna_gain />
<channel>1</channel>
<display_channel>1</display_channel>
<display_enabled>false</display_enabled>
<display_transmit_power>22.5 dBm</display_transmit_power>
<enabled>true</enabled>
<operational_mode>g</operational_mode>
<radio_interface>1</radio_interface>
<radio_mac>00:20:A6:55:E6:E0</radio_mac>
<radio_type>g</radio_type>
<transmit_power>100%</transmit_power>
</radio>
<reboot_count>2</reboot_count>
<serial_number>04UT43570051</serial_number>
<snmp_uptime>0</snmp_uptime>
<ssid>proxim_test</ssid>
<syscontact />
<syslocation />
<upstream_device_id>3648</upstream_device_id>
<upstream_port_index>16</upstream_port_index>
</ap>
<ap id="3648">
<controller_id>9</controller_id>
<device_category>thin_ap</device_category>
<firmware>4.0.155.0</firmware>
<group id="1">Access Points</group>
<is_up>true</is_up>
<lan_ip>10.51.1.16</lan_ip>
<lan_mac>00:0B:85:62:64:70</lan_mac>
<mfgr>Cisco</mfgr>
<model id="94">Aironet 1030 LWAPP</model>
<monitor_only>true</monitor_only>
<name>aironet-1030-5</name>
<operating_mode>ap</operating_mode>
<planned_maintenance_mode>false</planned_maintenance_mode>
<radio index="1">
<antenna>Enabled</antenna>
<antenna_gain>15</antenna_gain>
<antenna_type>internal</antenna_type>
<channel>1</channel>
<display_channel>1</display_channel>
<display_enabled>false</display_enabled>
<enabled>true</enabled>
<operational_mode>g</operational_mode>
<radio_interface>1</radio_interface>
<radio_mac>00:0B:85:62:64:70</radio_mac>
<radio_role>ap</radio_role>
<radio_type>g</radio_type>
<transmit_power>7 dBm</transmit_power>
</radio>
<radio index="2">
<antenna>Side B</antenna>
<antenna_gain />
<antenna_type>external</antenna_type>
<channel>56</channel>
<display_channel>56</display_channel>
<display_enabled>true</display_enabled>
<enabled>true</enabled>
<operational_mode>a</operational_mode>
<radio_interface>2</radio_interface>
<radio_mac>00:0B:85:62:64:70</radio_mac>
<radio_role>ap</radio_role>
<radio_type>abg</radio_type>
<transmit_power>5 dBm</transmit_power>
</radio>
<syscontact>dev team</syscontact>
<syscontact>Second shelf from bottom</syscontact>
<upstream_device_id />
<upstream_port_index />
</ap>
<ap id="2418">
<device_category>controller</device_category>
<firmware>6.1.1.1</firmware>
<group id="1">Access Points</group>
<is_up>true</is_up>
<lan_ip>10.51.3.119</lan_ip>
<lan_mac>00:0B:86:61:16:5C</lan_mac>
<mfgr>Aruba</mfgr>
<model id="226">3200</model>
<monitor_only>true</monitor_only>
<name>Aruba3200-119</name>
<operating_mode>ap</operating_mode>
<planned_maintenance_mode>false</planned_maintenance_mode>
<serial_number>AC0000479</serial_number>
<syscontact>qa team</syscontact>
<syslocation>Viewonly</syslocation>
<upstream_device_id />
<upstream_port_index />
</ap>
<ap id="1437">
<device_category>switch</device_category>
<firmware>12.2(25)SEE4</firmware>
<group id="1">Access Points</group>
<is_up>true</is_up>
<lan_ip>10.51.0.26</lan_ip>
<lan_mac>00:18:18:9E:C9:40</lan_mac>
<mfgr>Cisco</mfgr>
<model id="319">Catalyst 3560-24PS-S</model>
<monitor_only>true</monitor_only>
<name>switch7</name>
<operating_mode>ap</operating_mode>
<planned_maintenance_mode>false</planned_maintenance_mode>
<serial_number>CAT10205436</serial_number>
<syscontact />
<syslocation>server room</syslocation>
<upstream_device_id />
<upstream_port_index />
</ap>
</amp:amp_ap_list>
Upvotes: 0
Views: 119
Reputation: 23815
Below
import xml.etree.ElementTree as ET
xml = '''<?xml version="1.0" encoding="UTF-8"?>
<amp:amp_ap_list xmlns:amp="http://www.airwave.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1" xsi:schemaLocation="http://www.airwave.com amp_ap_list.xsd">
<ap id="79">
<altitude>0</altitude>
<device_category>fat_ap</device_category>
<client_count>1</client_count>
<firmware>3.2.0</firmware>
<fqdn />
<group id="1">Access Points</group>
<is_up>true</is_up>
<is_remote_ap>true</is_remote_ap>
<remote_outer_ip>10.1.1.1</remote_outer_ip>
<remote_lan_ip>20.20.20.20</remote_lan_ip>
<lan_ip>10.51.1.191</lan_ip>
<lan_mac>00:20:A6:55:E6:E1</lan_mac>
<last_contacted>1350379063</last_contacted>
<last_reboot>1343644897</last_reboot>
<mfgr>Proxim</mfgr>
<model id="44">AP-700</model>
<monitor_only>true</monitor_only>
<name>ORiNOCO-AP-700-55-e6-e1</name>
<operating_mode>ap</operating_mode>
<planned_maintenance_mode>false</planned_maintenance_mode>
<radio index="1">
<antenna />
<antenna_gain />
<channel>1</channel>
<display_channel>1</display_channel>
<display_enabled>false</display_enabled>
<display_transmit_power>22.5 dBm</display_transmit_power>
<enabled>true</enabled>
<operational_mode>g</operational_mode>
<radio_interface>1</radio_interface>
<radio_mac>00:20:A6:55:E6:E0</radio_mac>
<radio_type>g</radio_type>
<transmit_power>100%</transmit_power>
</radio>
<reboot_count>2</reboot_count>
<serial_number>04UT43570051</serial_number>
<snmp_uptime>0</snmp_uptime>
<ssid>proxim_test</ssid>
<syscontact />
<syslocation />
<upstream_device_id>3648</upstream_device_id>
<upstream_port_index>16</upstream_port_index>
</ap>
<ap id="3648">
<controller_id>9</controller_id>
<device_category>thin_ap</device_category>
<firmware>4.0.155.0</firmware>
<group id="1">Access Points</group>
<is_up>true</is_up>
<lan_ip>10.51.1.16</lan_ip>
<lan_mac>00:0B:85:62:64:70</lan_mac>
<mfgr>Cisco</mfgr>
<model id="94">Aironet 1030 LWAPP</model>
<monitor_only>true</monitor_only>
<name>aironet-1030-5</name>
<operating_mode>ap</operating_mode>
<planned_maintenance_mode>false</planned_maintenance_mode>
<radio index="1">
<antenna>Enabled</antenna>
<antenna_gain>15</antenna_gain>
<antenna_type>internal</antenna_type>
<channel>1</channel>
<display_channel>1</display_channel>
<display_enabled>false</display_enabled>
<enabled>true</enabled>
<operational_mode>g</operational_mode>
<radio_interface>1</radio_interface>
<radio_mac>00:0B:85:62:64:70</radio_mac>
<radio_role>ap</radio_role>
<radio_type>g</radio_type>
<transmit_power>7 dBm</transmit_power>
</radio>
<radio index="2">
<antenna>Side B</antenna>
<antenna_gain />
<antenna_type>external</antenna_type>
<channel>56</channel>
<display_channel>56</display_channel>
<display_enabled>true</display_enabled>
<enabled>true</enabled>
<operational_mode>a</operational_mode>
<radio_interface>2</radio_interface>
<radio_mac>00:0B:85:62:64:70</radio_mac>
<radio_role>ap</radio_role>
<radio_type>abg</radio_type>
<transmit_power>5 dBm</transmit_power>
</radio>
<syscontact>dev team</syscontact>
<syscontact>Second shelf from bottom</syscontact>
<upstream_device_id />
<upstream_port_index />
</ap>
<ap id="2418">
<device_category>controller</device_category>
<firmware>6.1.1.1</firmware>
<group id="1">Access Points</group>
<is_up>true</is_up>
<lan_ip>10.51.3.119</lan_ip>
<lan_mac>00:0B:86:61:16:5C</lan_mac>
<mfgr>Aruba</mfgr>
<model id="226">3200</model>
<monitor_only>true</monitor_only>
<name>Aruba3200-119</name>
<operating_mode>ap</operating_mode>
<planned_maintenance_mode>false</planned_maintenance_mode>
<serial_number>AC0000479</serial_number>
<syscontact>qa team</syscontact>
<syslocation>Viewonly</syslocation>
<upstream_device_id />
<upstream_port_index />
</ap>
<ap id="1437">
<device_category>switch</device_category>
<firmware>12.2(25)SEE4</firmware>
<group id="1">Access Points</group>
<is_up>true</is_up>
<lan_ip>10.51.0.26</lan_ip>
<lan_mac>00:18:18:9E:C9:40</lan_mac>
<mfgr>Cisco</mfgr>
<model id="319">Catalyst 3560-24PS-S</model>
<monitor_only>true</monitor_only>
<name>switch7</name>
<operating_mode>ap</operating_mode>
<planned_maintenance_mode>false</planned_maintenance_mode>
<serial_number>CAT10205436</serial_number>
<syscontact />
<syslocation>server room</syslocation>
<upstream_device_id />
<upstream_port_index />
</ap>
<ap id="143347">
<device_category>switch</device_category>
<firmware>12.2(25)SEE4</firmware>
<group id="1">Other Group Name</group>
<is_up>true</is_up>
<lan_ip>10.51.0.26</lan_ip>
<lan_mac>00:18:18:9E:C9:40</lan_mac>
<mfgr>Cisco</mfgr>
<model id="319">Catalyst 3560-24PS-S</model>
<monitor_only>true</monitor_only>
<name>switch7</name>
<operating_mode>ap</operating_mode>
<planned_maintenance_mode>false</planned_maintenance_mode>
<serial_number>CAT10205436</serial_number>
<syscontact />
<syslocation>server room</syslocation>
<upstream_device_id />
<upstream_port_index />
</ap>
</amp:amp_ap_list>'''
# Add more properties if you wish
INTERESTING_PROPERTIES = {'lan_ip', 'lan_mac', 'name'}
root = ET.fromstring(xml)
ap_access_points = [element for element in root.findall('.//ap') if element.find('./group').text == 'Access Points']
for ap in ap_access_points:
print('ap: {}'.format(ap.attrib['id']))
for child in list(ap):
if child.tag in INTERESTING_PROPERTIES:
print('\t{} --> {}'.format(child.tag, child.text))
output
ap: 79
lan_ip --> 10.51.1.191
lan_mac --> 00:20:A6:55:E6:E1
name --> ORiNOCO-AP-700-55-e6-e1
ap: 3648
lan_ip --> 10.51.1.16
lan_mac --> 00:0B:85:62:64:70
name --> aironet-1030-5
ap: 2418
lan_ip --> 10.51.3.119
lan_mac --> 00:0B:86:61:16:5C
name --> Aruba3200-119
ap: 1437
lan_ip --> 10.51.0.26
lan_mac --> 00:18:18:9E:C9:40
name --> switch7
Upvotes: 1