Заголовок ответа Content-Type: синтаксический анализ application / xop + xml и lxml.etree.fromstring - PullRequest
0 голосов
/ 16 мая 2019

У меня есть ответ от SOAP API, который имеет Content-Type: application / xop + xml. Я не уверен, насколько эффективно я могу использовать Response.text с lxml.etree.fromstring для использования xml.

Вот ответный текст

 --uuid:051145c9-9210-4e26-a390-d7cdd06b9f94
Content-Type: application/xop+xml; charset=UTF-8; type="text/xml"
Content-Transfer-Encoding: binary
Content-ID: <root.message@cxf.apache.org>

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><listResponse xmlns="http://www.strongmail.com/services/v2/schema"><objectId xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="UserId"><id>101</id></objectId><objectId xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="UserId"><id>102</id></objectId><objectId xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="UserId"><id>103</id></objectId><objectId xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="UserId"><id>107</id></objectId><objectId xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="UserId"><id>108</id></objectId><objectId xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="UserId"><id>109</id></objectId></listResponse></soap:Body></soap:Envelope>
--uuid:051145c9-9210-4e26-a390-d7cdd06b9f94--

Взяв .text и проанализировав etree.fromstring

from lxml import etree
resXML = etree.fromstring(theResponse.text)

Дает следующее:

    resXML = etree.fromstring(theResponse.text)
  File "src/lxml/etree.pyx", line 3222, in lxml.etree.fromstring
  File "src/lxml/parser.pxi", line 1877, in lxml.etree._parseMemoryDocument
  File "src/lxml/parser.pxi", line 1758, in lxml.etree._parseDoc
  File "src/lxml/parser.pxi", line 1068, in lxml.etree._BaseParser._parseUnicodeDoc
  File "src/lxml/parser.pxi", line 601, in lxml.etree._ParserContext._handleParseResultDoc
  File "src/lxml/parser.pxi", line 711, in lxml.etree._handleParseResult
  File "src/lxml/parser.pxi", line 640, in lxml.etree._raiseParseError
  File "<string>", line 1
lxml.etree.XMLSyntaxError: Start tag expected, '<' not found, line 1, column 1

Я полагаю, что это потому, что он ожидал '<' в первую очередь, поскольку все xml начинаются с этого. </p>

Я посмотрел в lxml.etree doc https://lxml.de/tutorial.html#parsing-from-strings-and-files и нашел .parse, но это только для файлов. Глядя на методы для Response, я вижу, что могу получить информацию о заголовках, например о типе контента, хотя документация продолжается с помощью json,

Есть ли какой-либо метод в Response, который может извлечь только часть xml, исключая заголовки, или есть один в lxml.etree?

1 Ответ

0 голосов
/ 16 мая 2019

Вы можете справиться с этим следующим образом:

theResponse = [your response above]

from lxml import etree
from io import StringIO

parser = etree.HTMLParser()
tree   = etree.parse(StringIO(theResponse), parser)

С этого момента lxml может справиться с этим. Для случайного примера, если вы ищете ссылки в ответе, вы можете попробовать:

for i in tree.iter():
if len(i.values())>0:
       print(i.values()[0])

И вывод будет:

http://schemas.xmlsoap.org/soap/envelope/
http://www.strongmail.com/services/v2/schema
http://www.w3.org/2001/XMLSchema-instance

и т.д.

...