XML Unicode strings with encoding declaration are not supported

前端 未结 3 1285
醉酒成梦
醉酒成梦 2020-12-09 02:50

Trying to do the following...

from lxml import etree
from lxml.etree import fromstring

if request.POST:
    parser = etree.XMLParser(ns_clean=True, recover=         


        
相关标签:
3条回答
  • 2020-12-09 03:28

    The following solution from kernc worked for me:

    from lxml import etree
    
    xml = u'<?xml version="1.0" encoding="utf-8" ?><foo><bar/></foo>'
    xml = bytes(bytearray(xml, encoding='utf-8'))  # ADDENDUM OF THIS LINE (when unicode means utf-8, e.g. on Linux)
    etree.XML(xml)
    
    # <Element html at 0x5b44c90>
    
    0 讨论(0)
  • 2020-12-09 03:43

    More simple than answers above:

    from lxml import etree
    
    #Do request for data, response = r#
    data = etree.fromstring(bytes(r.text, encoding='utf-8'))
    
    0 讨论(0)
  • 2020-12-09 03:50

    You'll have to encode it and then force the same encoding in the parser:

    from lxml import etree
    from lxml.etree import fromstring
    
    if request.POST:
        xml = request.POST['xml'].encode('utf-8')
        parser = etree.XMLParser(ns_clean=True, recover=True, encoding='utf-8')
        h = fromstring(xml, parser=parser)
    
        return HttpResponse(h.cssselect('delivery_reciept status').text_content())
    
    0 讨论(0)
提交回复
热议问题