Need help parsing XML with ElementTree

sagito3x

New Member
I'm trying to parse the following XML data:http://pastebin.com/UcbQQSM2This is just an example of the 2 types of data I will run into. Companies with the needed address information and companies without the needed information.From the data I need to collect 3 pieces of information:1) The Company name2) The Company street3) The Company zipcodeI'm able to do this with the following code:\[code\]#Creates list of Company namesCompanyList = []for company in xmldata.findall('company'): name = company.find('name').text CompanyList.append(name)#Creates list of Company zipcodesZipcodeList = []for company in xmldata.findall('company'): contact_data = http://stackoverflow.com/questions/14508213/company.find('contact-data') address1 = contact_data.find('addresses') for address2 in address1.findall('address'): ZipcodeList.append(address2.find('zip').text)#Creates list of Company streetsStreetList = []for company in xmldata.findall('company'): contact_data = http://stackoverflow.com/questions/14508213/company.find('contact-data') address1 = contact_data.find('addresses') for address2 in address1.findall('address'): StreetList.append(address2.find('street').text)\[/code\]But it doesn't really do what I want it to, and I can't figure out how to do what I want. I believe it will be some type of 'if' statement but I don't know.The problem is that where I have:\[code\]for address2 in address1.findall('address'): ZipcodeList.append(address2.find('zip').text)\[/code\]and \[code\]for address2 in address1.findall('address'): StreetList.append(address2.find('street').text)\[/code\]It only adds to the list the places that actually have a street name or zipcode listed in the XML, but I need a placemark for the companies that also DON'T have that information listed so that my lists match up.I hope this makes sense. Let me know if I need to add more information.But, basically, I'm trying to find a way to say if there isn't a zipcode/street name for the Company put "None" and if there is then put the zipcode/street name.Any help/guidance is appreciated.
 
Back
Top