2017-08-14 119 views
0

我需要一些幫助來進行迭代。我在XML中的根是sdnEntry。如果我在文檔中沒有任何迭代使用[0],我可以從中檢索文本值,但是當我在循環中收到像「last_names = sdns.getElementsByTagName(」lastName「).ErrorError:'NodeList '對象有沒有屬性 '的getElementsByTagName'」Python for Loop

我的工作代碼 - wihout任何迭代看起來是這樣的:

from xml.dom import minidom 
xmldoc = minidom.parse("/Users/cohen/Documents/project/sdn.xml") 
sdns = xmldoc.getElementsByTagName("sdnEntry")[0] 
last_names = sdns.getElementsByTagName("lastName")[0] 
ln = last_names.firstChild.data 
types = sdns.getElementsByTagName("sdnType")[0] 
t = types.firstChild.data 


programs = sdns.getElementsByTagName("programList")[0] #program.firstChild.data 
s = programs.getElementsByTagName("program")[0].firstChild.data 
akas = sdns.getElementsByTagName("akaList")[0] #child lastName.fourthChild.data 
a = akas.getElementsByTagName("aka")[0] 
a1 = a.getElementsByTagName("lastName")[0].firstChild.data 

addresses = sdns.getElementsByTagName("addressList")[0] 
ad1 = addresses.getElementsByTagName("address")[0] 
ad2 = ad1.getElementsByTagName("city")[0] 
city= ad2.firstChild.data 
ad3 = ad1.getElementsByTagName("country")[0] 
country = ad3.firstChild.data 

這是怎麼看起來像我的XML:

<sdnEntry> 
    <uid>36</uid> 
    <lastName>AEROCARIBBEAN AIRLINES</lastName> 
    <sdnType>Entity</sdnType> 
    <programList> 
     <program>CUBA</program> 
    </programList> 
    <akaList> 
     <aka> 
     <uid>12</uid> 
     <type>a.k.a.</type> 
     <category>strong</category> 
     <lastName>AERO-CARIBBEAN</lastName> 
     </aka> 
    </akaList> 
    <addressList> 
     <address> 
     <uid>25</uid> 
     <city>Havana</city> 
     <country>Cuba</country> 
     </address> 
    </addressList> 
    </sdnEntry> 

下面是我的循環。 請指教。先謝謝你!

for sdn in sdns: 
    for ln in last_names: 
     print(ln) 
     for t in types: 
      print(t) 
      for program in programs: 
       print (s) 
       for aka in akas: 
        print(a1) 
        for address in addresses: 
         print(city) 
         print(country) 

我需要每個sdnEntry存儲在我的數據庫,所以我需要爲每個條目只知道

  • <name> (lastName AEROCARIBBEAN AIRLINES)
  • <sdnType>(實體)`,
  • 來自程序列表的<programs>例如(CUBA程序),但他們可以更多,
  • <aka><lastName>(AERO-加勒比)全部
  • <address>的所有的人(哈瓦那市國家古巴)

我怎麼能這樣做?

回答

1
from xml.etree import ElementTree 

# I included this list to help 
all_nodes = ['sdnEntry', 'uid', 'lastName', 'sdnType', 'programList', 'program', 'akaList', 
      'aka', 'uid', 'type', 'category', 'lastName', 'addressList', 'address', 'uid', 
      'city', 'country'] 

required_nodes = ['lastName', 'uid', 'program', 'type', 'category', 'city', 'country'] 

# required because some names are repeated uid, last 
keys = ['sdnEntry_uid', 'lastName', 'program', 'aka_uid', 'type', 'category', 'aka_lastName', 
     'address_uid', 'city', 'country'] 

sdn_data = {} 
index = 0 

with open('stuff.xml', 'r') as xml_file: 
    tree = ElementTree.parse(xml_file) 

# iterate all nodes 
for node in tree.iter(): 
    # check if a required node 
    if node.tag in required_nodes: 
     # add to dictionary 
     sdn_data[keys[index]] = node.text 
     index += 1 

# Use this to test 
for key, value in sdn_data.items(): 
    print(key, value) 

輸出
sdnEntry_uid 36
lastName的AEROCARIBBEAN AIRLINES
程序CUBA
aka_uid 12
型又名
類強
aka_lastName AEROCARIBBEAN
address_uid 25
城市哈瓦那
國家古巴

+0

謝謝你的回答!這是一個進步。我需要將每個sdnEntry存儲在我的數據庫中,因此我需要每個條目只知道名稱(lastName AEROCARIBBEAN AIRLINES),(sdnType Entity),程序列表中的程序例如(方案古巴),但他們可以更多,所有的akas(姓氏AERO-CARIBBEAN)和所有地址(城市哈瓦那 國家古巴)我怎麼能做到這一點?謝謝迪克! – Cohen

+0

更新的代碼,它可以很容易地修改,以刪除不需要的項目 – diek

+0

上述邏輯應該工作,它是爲一個條目完成的。每完成一次輸入後,將索引重置爲0 – diek

0

不是一個真正的答案,但我可以建議嘗試xmltodict。 API處理IMO要容易得多,如果你確實遇到了錯誤,他們肯定會變得更加隱晦(即 - 因爲完整的結果有效載荷只是一個python字典,所以很容易查看事情可能已經消失的地方錯誤)。

+0

謝謝!也許是因爲他們看起來像列表並且沒有文本屬性? – Cohen