I need some help with an iteration. My root in XML is sdnEntry. If i use [0] without any iteration in the doc, i can retrieve the text value from it, but when i am doing the loop i receive errors like "last_names = sdns.getElementsByTagName("lastName"). AttributeError: 'NodeList' object has no attribute 'getElementsByTagName'"
My working code- wihout any iteration looks like this:
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
This is how it looks like my 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>
Below is my for loop. Please advise. Thank you in advance!
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)
I need to store each sdnEntry in my DB, therefore i need for each entry to know only the
<name> (lastName AEROCARIBBEAN AIRLINES),<sdnType>(Entity)`,<programs>from program list e.g. (program CUBA) but they can be more,<aka><lastName>(AERO-CARIBBEAN) all of them<address>all of them (city Havana country Cuba )
How can I do that?