2017-07-25 55 views
1

我有這種結構的XML文件:Python的minidom命名檢查元素存在

<?DOMParser ?> 
<logbook:LogBook xmlns:logbook="http://www/logbook/1.0" version="1.2"> 
<visits> 
<visit> 
    <general> 
     <technology>EB</technology> 
    </general> 
</visit> 
<visit> 
<general> 
    <grade>23242</grade> 
    <technology>EB</technology> 
</general> 
</visit> 
</visits> 
</logbook:LogBook> 

我要檢查,如果在visit標籤存在的每一列,如果它不存在,我想回到無,所以我寫了這段代碼:

import xml.dom.minidom as minidom 
mydict={} 
columnsLst=['grade','technology'] 
doc=minidom.parse('file.xml') 
visitcount=len(doc.getElementsByTagName('visit')) 
for i in range(visitcount): 
    for c in columnsLst: 
     if(doc.getElementsByTagName(c)[i].firstChild): 
     mydict[c]=doc.getElementsByTagName(c)[i].firstChild.data 
    print(mydict) 

這不起作用,因爲它對於不存在的元素不返回無。我得到index error,因爲grade首先不存在visit

我試圖this solution以及使用hasChild(),但它給了錯誤:

'Element' object has no attribute 'hasChild' 

任何這裏的想法?

回答

1

Question: minidom check element exists

代替具有指數fideling的使用產生的NodeLists,例如:

# Get List of Nodes with Tag <visit> 
visits = doc.getElementsByTagName('visit') 

# Iterate NodeList 
for n, visit in enumerate(visits, 1): 
    print('{}:{}'.format(n, visit)) 

    # Get SubNodes with Tag <general> 
    general = visit.getElementsByTagName('general') 

    # First Error Condition 
    if general: 
     # Iterate all Tag Names 
     for subtag in ['grade', 'technology']: 
      # Second Error Condition, assuming only ONE SubNode <general> 
      if not general[0].getElementsByTagName(subtag): 
       print('\tMissing Subtag <{}>'.format(subtag)) 
    else: 
     print('\tMissing Tag <general>') 

Output:

<Element {http://www/logbook/1.0}LogBook at 0xf707f52c> 
1:<DOM Element: visit at 0xf6a6125c> 
    Missing Subtag <grade> 
2:<DOM Element: visit at 0xf6a6184c> 

測試與Python:3.4.2