I am trying to generate XML file from PostgreSQL database table. I was able to generate using below code but it is not formatted and displaying in single line. While getting row from rows it displays complete file in single row. How can I fix this problem?
def tableToXML(message):
conn = None
try:
conn=psycopg2.connect("dbname='DB' user='user' password='i123456'")
except:
print "I am unable to connect to the database."
cur = conn.cursor()
try:
cur.execute("SELECT table_to_xml('usapp_sipconf', true, false, '')")
except:
print "I can't SELECT from sipconf"
rows = cur.fetchall()
with open('sipconf.xml', 'w') as f:
for row in rows:
print row
f.write("%s" % (str(row)))
if conn:
conn.close()
return True