gpt4 book ai didi

python - Django XML 无法加载外部实体

转载 作者:行者123 更新时间:2023-12-01 05:46:47 25 4
gpt4 key购买 nike

我收到一个名为 xml 的参数,其中包含一个 xml 文档。这是我尝试过的...

from lxml.cssselect import CSSSelector, etree
from lxml.etree import fromstring

if request.POST:

parser = etree.XMLParser(ns_clean=True, recover=True)
h = etree.parse(request.POST['xml'], parser)

return HttpResponse(h)

但我收到此错误:

[Fri Apr 05 09:50:13 2013] [error] IOError: Error reading file '<?xml version="1.0" encoding="ISO-8859-1"?>
[Fri Apr 05 09:50:13 2013] [error] <delivery_receipt>
[Fri Apr 05 09:50:13 2013] [error] <version>1.1</version>
[Fri Apr 05 09:50:13 2013] [error] <msisdn>447777111111</msisdn>
[Fri Apr 05 09:50:13 2013] [error] <submission_ref>12345678901234567890123456789012</submission_ref>
[Fri Apr 05 09:50:13 2013] [error] <status>Delivered</status>
[Fri Apr 05 09:50:13 2013] [error] <reason>4</reason>
[Fri Apr 05 09:50:13 2013] [error] <gmt_timestamp>20130405095100</gmt_timestamp>
[Fri Apr 05 09:50:13 2013] [error] <retry>0</retry>
[Fri Apr 05 09:50:13 2013] [error] </delivery_receipt>': failed to load external entity "<?xml version="1.0" encoding="ISO-8859-1"?>
[Fri Apr 05 09:50:13 2013] [error] <delivery_receipt>
[Fri Apr 05 09:50:13 2013] [error] <version>1.1</version>
[Fri Apr 05 09:50:13 2013] [error] <msisdn>447777111111</msisdn>
[Fri Apr 05 09:50:13 2013] [error] <submission_ref>12345678901234567890123456789012</submission_ref>

等等

有什么想法吗?谢谢:)

最佳答案

request.POST 默认返回 unicode 字符串。您遇到的下一个问题与您提供的编码与您提供的文档中声明的编码不匹配有关。

doc = request.POST.get('xml','')
if not doc:
raise Exception()
h = etree.XML(doc.encode('ascii'), parser)

关于python - Django XML 无法加载外部实体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15829814/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com