Python For Loop over XML -


i need iteration. root in xml sdnentry. if use [0] without iteration in doc, can retrieve text value it, when doing loop receive errors "last_names = sdns.getelementsbytagname("lastname"). attributeerror: 'nodelist' object has no attribute 'getelementsbytagname'"

my working code- wihout iteration looks 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 = 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 how looks 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 loop. please advise. thank in advance!

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

i need store each sdnentry in db, therefore need each entry know

  • <name> (lastname aerocaribbean airlines),
  • <sdntype> (entity)`,
  • <programs> program list e.g. (program cuba) can more,
  • <aka><lastname> (aero-caribbean) of them
  • <address> of them (city havana country cuba )

how can that?

from xml.etree import elementtree  # included list 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 names repeated uid, last keys = ['sdnentry_uid', 'lastname', 'program', 'aka_uid', 'type', 'category', 'aka_lastname',         'address_uid', 'city', 'country']  sdn_data = {} index = 0  open('stuff.xml', 'r') xml_file:     tree = elementtree.parse(xml_file)  # iterate nodes node in tree.iter():     # check if required node     if node.tag in required_nodes:         # add dictionary         sdn_data[keys[index]] = node.text         index += 1  # use test key, value in sdn_data.items():     print(key, value) 

output
sdnentry_uid 36
lastname aerocaribbean airlines
program cuba
aka_uid 12
type a.k.a.
category strong
aka_lastname aero-caribbean
address_uid 25
city havana
country cuba


Comments

Popular posts from this blog

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

python Tkinter Capturing keyboard events save as one single string -

sql server - Why does Linq-to-SQL add unnecessary COUNT()? -