Итак, у меня есть правильно сформированный XML, встроенный в zip-файл
<?xml version="1.0" encoding="utf-8"?>
<board>
<columns>
<c name="Work" position="1">
<tasks>
<t id="9b860ebd-a18f-4944-bc0c-e6846c03a5a2" />
</tasks>
</c>
<c name="Home" position="2">
<tasks>
<t id="6d6c6b90-5f06-49fe-90ea-50227c90bd8c" />
</tasks>
</c>
<c name="Fun" position="3">
<tasks>
<t id="bd5f7e33-1011-4c96-8022-900dad135145" />
</tasks>
</c>
<c name="Empty column" position="4">
<tasks>
</tasks>
</c>
</columns>
</board>
Когда этот файл анализируется, а не внедряется в архив, lxml не выдает ошибку синтаксического анализа / синтаксиса (это также "работает" сстандартный питон ElementTree).Мысль это было связано со сжатием, но нет.
Работа (не в архиве):
import lxml.etree as etree
# Yes, I could parse the file directly but wanted to check xml type
with open("board.xml", "r") as bo:
xml = bytes(bo.read(), "utf8")
e = etree.fromstring(xml)
Не работает (в архиве):
import zipfile
import lxml.etree as etree
# import xml.etree.ElementTree as etree
# Setting compression argument as zipfile.ZIP_DEFLATED because the archive's
# files were compressed that way changed nothing.
with zipfile.ZipFile("board.zip", "r") as boardzip:
manifest_xml = boardzip.read("board.xml") or False
# As seen from above, lxml.fromstring requires a bytes object. However, in
# the case of the embedded file, ZipFile.read already return a bytes
# object. Also, manifest_xml have a well-formed content.
if manifest_xml:
mxml = etree.fromstring(manifest_xml)
последние выходные данные кода:
# With LXML
lxml.etree.XMLSyntaxError: expected '>', line 7, column 10
# With standard python
xml.etree.ElementTree.ParseError: mismatched tag: line 7, column 8
Поскольку несоответствующий тег - </tasks>
, возможно, это был <t>
, который был проанализирован неправильно.Но преобразование <t id="9b860ebd-a18f-4944-bc0c-e6846c03a5a2" />
в <t id="9b860ebd-a18f-4944-bc0c-e6846c03a5a2"></t>
также ничего не изменило.
Есть идеи?[и извините за возможный ломаный английский]