来自 StringIO 源的 Python xml etree DTD?
我正在调整以下代码(通过这个问题中的建议创建),这需要XML 文件及其 DTD 并将它们转换为不同的格式。对于这个问题,只有加载部分很重要:
xmldoc = open(filename)
parser = etree.XMLParser(dtd_validation=True, load_dtd=True)
tree = etree.parse(xmldoc, parser)
在使用文件系统时,这工作得很好,但我将其转换为通过 Web 框架运行,其中两个文件通过表单加载。
加载 xml 文件工作正常:
tree = etree.parse(StringIO(data['xml_file'])
但是由于 DTD 链接到 xml 文件的顶部,因此以下语句失败:
parser = etree.XMLParser(dtd_validation=True, load_dtd=True)
tree = etree.parse(StringIO(data['xml_file'], parser)
通过 这个问题,我尝试过:
etree.DTD(StringIO(data['dtd_file'])
tree = etree.parse(StringIO(data['xml_file'])
虽然第一行不会导致错误,但第二行落在unicode实体上,DTD意味着拾取(并在文件系统版本中这样做):
XMLSyntaxError:实体“eacute”不是 已定义,第 4495 行,第 46 列
我如何正确加载此 DTD?
I'm adapting the following code (created via advice in this question), that took an XML file and it's DTD and converted them to a different format. For this problem only the loading section is important:
xmldoc = open(filename)
parser = etree.XMLParser(dtd_validation=True, load_dtd=True)
tree = etree.parse(xmldoc, parser)
This worked fine, whilst using the file system, but I'm converting it to run via a web framework, where the two files are loaded via a form.
Loading the xml file works fine:
tree = etree.parse(StringIO(data['xml_file'])
But as the DTD is linked to in the top of the xml file, the following statement fails:
parser = etree.XMLParser(dtd_validation=True, load_dtd=True)
tree = etree.parse(StringIO(data['xml_file'], parser)
Via this question, I tried:
etree.DTD(StringIO(data['dtd_file'])
tree = etree.parse(StringIO(data['xml_file'])
Whilst the first line doesn't cause an error, the second falls over on unicode entities the DTD is meant to pick up (and does so in the file system version):
XMLSyntaxError: Entity 'eacute' not
defined, line 4495, column 46
How do I go about correctly loading this DTD?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是一个简短但完整的示例,使用 @Steven 提到的自定义解析器技术。
Here's a short but complete example, using the custom resolver technique @Steven mentioned.
您可能可以使用自定义解析器。文档实际上给出了一个这样做的示例来提供 dtd。
You could probably use a custom resolver. The docs actually give an example of doing this to provide a dtd.