[Answered ]-Django XML failed to load external entity

1👍

request.POST returns unicode strings by default. The next problem you are having is related to the encoding your supplying isn’t matching the declared encoding in the document you’re supplying.

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

1👍

etree.parse expects a filename (or a file object). There’s no file named <all your xml>.

You need to feed the XML to the parser:

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

if request.POST:

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

Or use fromstring or XML functions.

h = fromstring(request.POST['xml'], parser=parser)

or

h = etree.XML(request.POST['xml'], parser=parser)

The lxml.etree tutorial

Leave a comment