Marko Nestorovic
Marko Nestorovic

Reputation: 23

Append XML responses in Python

I am trying to parse multiple XML responses in one file. However, when I write a responses to file, it shows only last one. I assume I need to add append somewhere in order to keep all responses.

Here is my code:

import json
import xml.etree.ElementTree as ET

#loop test 
feins = ['800228936', '451957238']



for i in feins:
   rr = requests.get('https://pdb-services.nipr.com/pdb-xml-reports/hitlist_xml.cgi?report_type=0&id_fein={}'.format(i),auth=('test', 'test'))

   root = ET.fromstring(rr.text)
   tree = ET.ElementTree(root)

tree.write("file.xml")

Upvotes: 0

Views: 191

Answers (1)

Jack Fleeting
Jack Fleeting

Reputation: 24940

Try changing

for i in feins:
   ...
   tree = ET.ElementTree(root)

tree.write("file.xml")

to (note the indentation):

for i in feins:
   ...
   tree = ET.ElementTree(root)
   with open("file.xml", "wb") as f:
      tree.write(f)

and see if it works.

Upvotes: 1

Related Questions