我试图将Oracle 12c数据库中保存的表导出为组成XML文件,以便Oracle表中的每一行生成1个XML文件。为此,我正在使用Python 2.7库xml.etree.ElementTree
,但我在documentation中看不到任何可以让我这样做的任何内容。基本上我需要在这一点上是:使用Python将Oracle数据库表导出为XML文件?
import cx_Oracle
from xml.etree import ElementTree as ET
SQL = ''.join([ 'SELECT * FROM ', table_name ])
connection = cx_Oracle.connect('username/[email protected]')
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()
我知道它只会是1行代码 - 我真的不知道该怎么办。
作为一个增加的复杂性,我不幸只能使用Python 2.7本地库,如etree - 我无法在工作中下载第三方Python库。在此先感谢您的任何帮助或建议。
'write()'是'ElementTree'实例上的方法,但您可以在类上调用它。表示您的表格行的XML结构的根元素应该是一个参数。 'ET.ElementTree(root).write(file)'是否工作(假设'root'是那个根元素)? – mzjn