将XML元素从Oracle数据库传递到ElementTree(Python XML Parser)的问题
我想从数据库中获取XML文件,用ElementTree对其进行操作,然后将其插入另一个数据库。使文件工作正常,我可以全部打印它。但是,每当我尝试通过解析器获取它时,它都会返回错误“未找到元素”。
这是代码:
import cx_Oracle
import xml.etree.ElementTree as et
cx_Oracle.init_oracle_client(lib_dir=r"here and there")
try:
dsn_tns_source = cx_Oracle.makedsn('censored for obvious reasons')
con_source = cx_Oracle.connect(cx_Oracle.makedsn('same here'))
except cx_Oracle.DatabaseError as err:
print("Connection DB error:", err)
try:
cur_source = con_source.cursor()
source_select = cur_source.execute("working SELECT")
print(source_select)
for row in source_select:
x = row[(len(row) - 1)] # This is the XML
print("source_row: ", x)
tree = et.parse(x)
root = tree.getroot()
print(root)
print(et.tostring(root, encoding='utf-8').decode('utf-8'))
for col in cur_source.description:
print("source_col: ", col)
显然我没有正确地传递“ X”,但是,整个XML应该在调用该变量时有所帮助。大多数教程仅显示如何插入本地文件,因此我认为简单使用该变量就足够了。 错误消息如下:
Traceback (most recent call last):
File "Z:\basler_benchmark\main.py", line 24, in <module>
tree = et.parse(x)
File "C:\Python\lib\xml\etree\ElementTree.py", line 1229, in parse
tree.parse(source, parser)
File "C:\Python\lib\xml\etree\ElementTree.py", line 580, in parse
self._root = parser._parse_whole(source)
xml.etree.ElementTree.ParseError: no element found: line 1, column 0
i want to get an XML file out of a database, manipulate it with ElementTree and then insert it into another database. Getting the file works just fine, i can print it in it's entirety. However, whenever i try to get it through the parser it returns the error "no element found".
Here is the code:
import cx_Oracle
import xml.etree.ElementTree as et
cx_Oracle.init_oracle_client(lib_dir=r"here and there")
try:
dsn_tns_source = cx_Oracle.makedsn('censored for obvious reasons')
con_source = cx_Oracle.connect(cx_Oracle.makedsn('same here'))
except cx_Oracle.DatabaseError as err:
print("Connection DB error:", err)
try:
cur_source = con_source.cursor()
source_select = cur_source.execute("working SELECT")
print(source_select)
for row in source_select:
x = row[(len(row) - 1)] # This is the XML
print("source_row: ", x)
tree = et.parse(x)
root = tree.getroot()
print(root)
print(et.tostring(root, encoding='utf-8').decode('utf-8'))
for col in cur_source.description:
print("source_col: ", col)
Apparently I am not passing "x" correctly, however, the entire XML should be help in that variable at the point of calling it. Most tutorials only show how to insert local files so i thought simply using the variable would be sufficient.
The error message is the following:
Traceback (most recent call last):
File "Z:\basler_benchmark\main.py", line 24, in <module>
tree = et.parse(x)
File "C:\Python\lib\xml\etree\ElementTree.py", line 1229, in parse
tree.parse(source, parser)
File "C:\Python\lib\xml\etree\ElementTree.py", line 580, in parse
self._root = parser._parse_whole(source)
xml.etree.ElementTree.ParseError: no element found: line 1, column 0
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
.parse()
专门解析文件。如果
x
是字符串,则使用.fromstring()
:如果
x
是其他内容,则必须先将其转换为字符串。对于 cx_Oracle.LOB 对象,调用 .read() 应该可以解决问题:.parse()
specifically parses files.If
x
is a string, use.fromstring()
:If
x
is something else, it must be turned into a string first. Forcx_Oracle.LOB
objects, calling.read()
should do the trick:错误是
导入它
a)我在x中的不是一个XML文件而是一个LOB,我必须像Tomalak回答/评论一样
, b)我没有意识到你必须迭代整个事情为了能够使用 .text 来获取字段/标签内的内容。
所以解决方案如下所示:
The error was
a) That what I had in x was not an XML file but a LOB and that i had to import it with
like Tomalak answered/commented and
b) that I didn't realize that you HAVE TO iterate over the entire thing in order to be able to use .text to get what's inside the field/tag.
So the solution looks like this: