I'm trying to export a table held within an Oracle 12c database into constituent XML files, such that each row in the Oracle table produces 1 XML file. To do this, I am using the Python 2.7 library xml.etree.ElementTree, but I can't see anything in the documentation which would allow me to do this. Essentially all I require at this point is:
import cx_Oracle
from xml.etree import ElementTree as ET
SQL = ''.join([ 'SELECT * FROM ', table_name ])
connection = cx_Oracle.connect('username/password@database')
cursor = connection.cursor()
for i in range(num_rows):
... #code works fine up to here
file_name = i
file_path = ''.join([ 'C:\..., file_name, '.xml ])
file = open(file_path, 'w')
cursor.execute(SQL)
ET.ElementTree.write(file) #This line won't work
cursor.close()
file.close()
connection.close()
I know it will only be 1 line of code - I'm just really not sure what to do.
As an added complication, I can unfortunately only use libraries native to Python 2.7 such as etree - I'm not able to download 3rd party Python libraries at work. Thanks in advance for any help or advice.
write()is a method onElementTreeinstances, but you call it on the class. And the root element of the XML structure that represents your table row should be an argument. DoesET.ElementTree(root).write(file)work (assuming thatrootis that root element)?