如何将xml文件传递给lxml进行解析?

发布于 2024-09-04 15:33:49 字数 1255 浏览 4 评论 0原文

我正在尝试使用 lxml 解析 xml 文件。 xml.etree 允许我简单地将文件名作为参数传递给 parse 函数,因此我尝试对 lxml 执行相同的操作。

我的代码:

from lxml import etree
from lxml import objectify

file = "C:\Projects\python\cb.xml"
tree = etree.parse(file)

但我收到错误:

Traceback (most recent call last):
  File "cb.py", line 5, in <module>
    tree = etree.parse(file)
  File "lxml.etree.pyx", line 2698, in lxml.etree.parse (src/lxml/lxml.etree.c:4
9590)
  File "parser.pxi", line 1491, in lxml.etree._parseDocument (src/lxml/lxml.etre
e.c:71205)
  File "parser.pxi", line 1520, in lxml.etree._parseDocumentFromURL (src/lxml/lx
ml.etree.c:71488)
  File "parser.pxi", line 1420, in lxml.etree._parseDocFromFile (src/lxml/lxml.e
tree.c:70583)
  File "parser.pxi", line 975, in lxml.etree._BaseParser._parseDocFromFile (src/
lxml/lxml.etree.c:67736)
  File "parser.pxi", line 539, in lxml.etree._ParserContext._handleParseResultDo
c (src/lxml/lxml.etree.c:63820)
  File "parser.pxi", line 625, in lxml.etree._handleParseResult (src/lxml/lxml.e
tree.c:64741)
  File "parser.pxi", line 565, in lxml.etree._raiseParseError (src/lxml/lxml.etr
ee.c:64084)
lxml.etree.XMLSyntaxError: AttValue: " or ' expected, line 2, column 26

我做错了什么?

I'm trying to parse an xml file using lxml. xml.etree allowed me to simply pass the file name as a parameter to the parse function, so I attempted to do the same with lxml.

My code:

from lxml import etree
from lxml import objectify

file = "C:\Projects\python\cb.xml"
tree = etree.parse(file)

but I get the error:

Traceback (most recent call last):
  File "cb.py", line 5, in <module>
    tree = etree.parse(file)
  File "lxml.etree.pyx", line 2698, in lxml.etree.parse (src/lxml/lxml.etree.c:4
9590)
  File "parser.pxi", line 1491, in lxml.etree._parseDocument (src/lxml/lxml.etre
e.c:71205)
  File "parser.pxi", line 1520, in lxml.etree._parseDocumentFromURL (src/lxml/lx
ml.etree.c:71488)
  File "parser.pxi", line 1420, in lxml.etree._parseDocFromFile (src/lxml/lxml.e
tree.c:70583)
  File "parser.pxi", line 975, in lxml.etree._BaseParser._parseDocFromFile (src/
lxml/lxml.etree.c:67736)
  File "parser.pxi", line 539, in lxml.etree._ParserContext._handleParseResultDo
c (src/lxml/lxml.etree.c:63820)
  File "parser.pxi", line 625, in lxml.etree._handleParseResult (src/lxml/lxml.e
tree.c:64741)
  File "parser.pxi", line 565, in lxml.etree._raiseParseError (src/lxml/lxml.etr
ee.c:64084)
lxml.etree.XMLSyntaxError: AttValue: " or ' expected, line 2, column 26

What am I doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

小巷里的女流氓 2024-09-11 15:33:49

你做错的是(1)没有检查在同一个文件上使用xml.etree是否得到了相同的结果(2)没有读取错误消息,这表明第2行中有语法错误文件的任何文件打开问题的下游

What you are doing wrong is (1) not checking whether you got the same outcome by using xml.etree on the same file (2) not reading the error message, which indicates a syntax error in line 2 of the file, way down stream from any file-opening issue

孤凫 2024-09-11 15:33:49

今天早上我偶然发现了一条类似的错误消息,对我来说答案是格式错误的 DTD。在我的 DTD 中,有一个属性定义,其默认值没有用引号引起来 - 一旦我更改了它,错误就不再发生。

I stumbled across a similar error message this morning, and for me the answer was a malformed DTD. In my DTD, there was an Attribute definition with a default value that was not enclosed in quotes - as soon as I changed that, the error didn't happen anymore.

无可置疑 2024-09-11 15:33:49

您的 XML 标记 中存在语法错误。你没有做错任何事。

You have a syntax error in your XML Markup. You aren't doing anything wrong.

路还长,别太狂 2024-09-11 15:33:49

lxml 允许您通过使用 recover=True

etree.XMLParser(recover=True)

虽然这并不理想,但我使用它来加载 xml 以进行 schema/dtd/schematron 验证。

lxml allows you load a broken xml by creating a parser instance with recover=True

etree.XMLParser(recover=True)

While this is not ideal, I use this to load an xml for schema/dtd/schematron validation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文