Parsing XML and read the text of the tag using ElementTree using Python

import xml.etree.ElementTree as ET
import itertools
import os
tree = ET.parse('C:\\Users\\Administrator\\Desktop\\API testing\\Gordon_Seed_03212018.xml')
count = 0
for elem in tree.findall(".//additonalTradeItemID"):
if (elem.get("type") == "DISTRIBUTOR"):
count+= 1
print(count)
# Finding root element of the xml
root = tree.getroot()
prodstatusArray = []
for productStatus in root.iter('productStatus'):
prodstatusArray.append(productStatus.text )
itemidArray = []
for additonalTradeItemID in root.iter('additonalTradeItemID'):
if(additonalTradeItemID.attrib['type'] == 'DISTRIBUTOR'):
itemidArray.append((additonalTradeItemID.text ))
# for item in itertools.chain(prodstatusArray,itemidArray ):
# print(item)
new_list = []
for each in range(0,len(itemidArray)):
new_list.append(str(itemidArray[each]) + '----->'+ prodstatusArray[each])
outputfile = open('C:\\Users\\Administrator\\Desktop\\API testing\\xmloutfile.txt', 'w')
for i in range(len(new_list)):
outputfile.write(new_list[i])
outputfile.write('\n')

Comments

Popular Posts