2016-12-02 53 views
0

我一直在嘗試這和關閉的最後兩個星期,並通過python的文件進行XML解析。我仍然可以弄清楚它是否是Xpath的事情。如果任何人都可以提供一些幫助,我將不勝感激。Python的XML解析兒童和大的孩子的屬性

我的XML文件有很多孩子,我正在使用root.findall()來獲取myAccessPoints的屬性,然後在它下面有三個孩子我有一個具有我想提取的幾個屬性的元素。然而,到目前爲止,我只能用2 for循環做到這一點。

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 

下面是XML文件的例子:

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

最後我想借此接入點元素的屬性,像這樣 - >

的accessPoint ID

accessPointMeasurement MAC, ssid,primaryNumber

技術帶

技術帶

技術帶

一些的accessPoint元素有2臺收音機,所以我就一定得accessPointMeasurement屬性的兩倍。

我想我將不得不創建一個類,並在課堂內,我不得不讓自己的列表或字典。

我不是要求任何人爲我做任何事情,除了瞭解我如何獲得每個接入點並且它是一個for循環中的屬性(如果甚至可能的話)。

感謝您的幫助。

回答

0

我擴展了您的XML以包含更多accessPointsradios,並使用lxml庫訪問其xpath功能。嵌套循環。

from lxml import etree 

tree = etree.parse('temp.xml') 
accessPoints = tree.xpath('.//accessPoint') 

for accessPoint in accessPoints: 
    print ('accessPoint id:', accessPoint.attrib['id']) 
    radios = accessPoint.xpath('radio') 
    for radio in radios: 
     accessPointMeasurement = radio.xpath('accessPointMeasurement') 
     print ('\taccessPointMeasurement: ', accessPointMeasurement[0].attrib) 
     technologies = radio.xpath('.//technology') 
     for technology in technologies: 
      print ('\t\ttechnology: ', technology.attrib) 

結果是這些:

accessPoint id: 0 
    accessPointMeasurement: {'security': 'WPA2', 'informationElements': '000445676773010882840b162430486c0301072a01042f010430140100000fac040100000fac040100000fac020c0032040c1218602d1afc181fffff0000000000000000000000000000000000000000003d16070017000000000000000000000000000000000000004a0e14000a002c01c8001400050019007f0101dd890050f204104a0001101044000102103b00010310470010177b8b3ae292d7c44b93d4616ff30e7e1021000d4e4554474541522c20496e632e1023000a574e44523334303076331024000a574e44523334303076331042000230311054000800060050f20400011011000a574e4452333430307633100800020004103c0001031049000600372a000120dd090010180204f0040000dd180050f2020101800003a4000027a4000042435e0062322f00', 'bandwidthMhz': '20', 'centerNumber': '7', 'mac': 'a0:63:91:21:c4:f8', 'ssid': 'Eggs', 'primaryFrequencyMhz': '2442', 'primaryNumber': '7'} 
     technology: {'band': '802.11g'} 
     technology: {'band': '802.11b'} 
     technology: {'band': '802.11n'} 
accessPoint id: 2 
    accessPointMeasurement: {'security': 'WPA2', 'informationElements': '000445676773010882840b162430486c0301072a01042f010430140100000fac040100000fac040100000fac020c0032040c1218602d1afc181fffff0000000000000000000000000000000000000000003d16070017000000000000000000000000000000000000004a0e14000a002c01c8001400050019007f0101dd890050f204104a0001101044000102103b00010310470010177b8b3ae292d7c44b93d4616ff30e7e1021000d4e4554474541522c20496e632e1023000a574e44523334303076331024000a574e44523334303076331042000230311054000800060050f20400011011000a574e4452333430307633100800020004103c0001031049000600372a000120dd090010180204f0040000dd180050f2020101800003a4000027a4000042435e0062322f00', 'bandwidthMhz': '20', 'centerNumber': '7', 'mac': 'a0:63:91:21:c4:f8', 'ssid': 'Eggs', 'primaryFrequencyMhz': '2442', 'primaryNumber': '7'} 
     technology: {'band': '802.11g'} 
     technology: {'band': '802.11b'} 
     technology: {'band': '802.11n'} 
    accessPointMeasurement: {'security': 'WPA2', 'informationElements': '000445676773010882840b162430486c0301072a01042f010430140100000fac040100000fac040100000fac020c0032040c1218602d1afc181fffff0000000000000000000000000000000000000000003d16070017000000000000000000000000000000000000004a0e14000a002c01c8001400050019007f0101dd890050f204104a0001101044000102103b00010310470010177b8b3ae292d7c44b93d4616ff30e7e1021000d4e4554474541522c20496e632e1023000a574e44523334303076331024000a574e44523334303076331042000230311054000800060050f20400011011000a574e4452333430307633100800020004103c0001031049000600372a000120dd090010180204f0040000dd180050f2020101800003a4000027a4000042435e0062322f00', 'bandwidthMhz': '20', 'centerNumber': '7', 'mac': 'a0:63:91:21:c4:f8', 'ssid': 'Eggs', 'primaryFrequencyMhz': '2442', 'primaryNumber': '7'} 
     technology: {'band': '802.11g'} 
     technology: {'band': '802.11b'} 
     technology: {'band': '802.11n'} 
+0

感謝您的幫助比爾。看起來我需要更多地瞭解Xpaths。 – James

+0

根據我在過去幾天給出的一個或兩個xpath答案來判斷,我可以學到更多!如果主要來自程序背景,我覺得有點棘手。這可能比我在這裏提供的更好。順便說一句,如果你重視SO的話,一旦你有足夠的聲望,你就可以對它投票。人們喜歡那樣。 –

+0

我不是經常在stackoverflow上,我不知道你是否有興趣,但我有一個github帳戶與這個項目了。 我會提前提醒你......我是初學者,所以我的代碼可能不會被讀取,這可能只是浪費你的時間。 [我的GitHub(https://github.com/HH0718/Ekahau_Parse) SO不會讓我投票,但我打上這個崗位作爲回答。 謝謝比爾。 – James