将XML元素从Oracle数据库传递到ElementTree(Python XML Parser)的问题

发布于 2025-01-20 20:34:08 字数 1455 浏览 4 评论 0原文

我想从数据库中获取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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

终陌 2025-01-27 20:34:08

.parse() 专门解析文件。

如果x是字符串,则使用.fromstring()

root = et.fromstring(x)

如果x是其他内容,则必须先将其转换为字符串。对于 cx_Oracle.LOB 对象,调用 .read() 应该可以解决问题:

root = et.fromstring(x.read())

.parse() specifically parses files.

If x is a string, use .fromstring():

root = et.fromstring(x)

If x is something else, it must be turned into a string first. For cx_Oracle.LOB objects, calling .read() should do the trick:

root = et.fromstring(x.read())
仅此而已 2025-01-27 20:34:08

错误是

导入它

et.fromstring(x.read())

a)我在x中的不是一个XML文件而是一个LOB,我必须像Tomalak回答/评论一样

, b)我没有意识到你必须迭代整个事情为了能够使用 .text 来获取字段/标签内的内容。

所以解决方案如下所示:

    for row in source_select:
        x = row[(len(row) - 1)]
        tree = et.ElementTree(et.fromstring(x.read()))
        root = tree.getroot()
        for aref in root.iter('name_of_tag'):
            print(aref.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

et.fromstring(x.read())

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:

    for row in source_select:
        x = row[(len(row) - 1)]
        tree = et.ElementTree(et.fromstring(x.read()))
        root = tree.getroot()
        for aref in root.iter('name_of_tag'):
            print(aref.text)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文