python - XML Unicode strings with encoding declaration are not supported -
trying following...
from lxml import etree lxml.etree import fromstring if request.post: parser = etree.xmlparser(ns_clean=true, recover=true) h = fromstring(request.post['xml'], parser=parser) return httpresponse(h.cssselect('itagg_delivery_receipt status').text_content())
but give error:
[fri apr 05 10:27:54 2013] [error] internal server error: /sms/status_postback/ [fri apr 05 10:27:54 2013] [error] traceback (most recent call last): [fri apr 05 10:27:54 2013] [error] file "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py", line 115, in get_response [fri apr 05 10:27:54 2013] [error] response = callback(request, *callback_args, **callback_kwargs) [fri apr 05 10:27:54 2013] [error] file "/usr/local/lib/python2.7/dist-packages/django/views/decorators/csrf.py", line 77, in wrapped_view [fri apr 05 10:27:54 2013] [error] return view_func(*args, **kwargs) [fri apr 05 10:27:54 2013] [error] file "/srv/project/livewiresms/sms/views.py", line 42, in update_delivery_status [fri apr 05 10:27:54 2013] [error] h = fromstring(request.post['xml'], parser=parser) [fri apr 05 10:27:54 2013] [error] file "lxml.etree.pyx", line 2754, in lxml.etree.fromstring (src/lxml/lxml.etree.c:54631) [fri apr 05 10:27:54 2013] [error] file "parser.pxi", line 1569, in lxml.etree._parsememorydocument (src/lxml/lxml.etree.c:82659) [fri apr 05 10:27:54 2013] [error] valueerror: unicode strings encoding declaration not supported.
this xml
<?xml version="1.1" encoding="iso-8859-1"?> <itagg_delivery_receipt> <version>1.0</version> <msisdn>447889000000</msisdn> <submission_ref> 845tgrgsehg394g3hdfhhh56445y7ts6</ submission_ref> <status>delivered</status> <reason>4</reason> <timestamp>20050709120945</timestamp> <retry>0</retry> </itagg_delivery_receipt>
i don't have control on xml document comes sms company.
you'll have encode , force same encoding in parser:
from lxml import etree 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())
Comments
Post a Comment