Python-zeep: Convert Zeep object to XML representation

Created on 22 Jun 2017  路  6Comments  路  Source: mvantellingen/python-zeep

Hi there, Zeep is a great project! Many thanks for building it.

Is it possible to inspect the XML that will be produced by a Zeep object without actually sending it off to an operation endpoint? This would be a useful tool for debugging.

If this is not already possible, this would make for a useful helper function.

Most helpful comment

It is possible. Instead of doing this:

client.service.MyService(params,_soapheaders=headers)

do this:

from lxml import etree as ET
#
#<your setup code for zeep here>
#
node = client.create_message(client.service, 'MyService', params = params, _soapheaders=headers)
tree = ET.ElementTree(node)
tree.write('test.xml',pretty_print=True)

All 6 comments

It is possible. Instead of doing this:

client.service.MyService(params,_soapheaders=headers)

do this:

from lxml import etree as ET
#
#<your setup code for zeep here>
#
node = client.create_message(client.service, 'MyService', params = params, _soapheaders=headers)
tree = ET.ElementTree(node)
tree.write('test.xml',pretty_print=True)

Hello I followed the example from the above link to create a raw xml for my wsdl. I couldn't generate any raw xml. I modified the request slightly to have None as the 3rd argument.

from zeep import Client

client = Client('http://my-endpoint.com/production.svc?wsdl')
node = client.create_message(client.service, 'myOperation', None)
print(node)

The response is as follows:

Can you please tell what's my mistake?

@prasanth-varikallu I'm _very_ new to zeep, so I may not be recommending the best practice, but you can try a couple approaches:

  • Simple:
    from zeep.wsdl.utils import etree_to_string
    etree_to_string(soap_message).decode()
  • Approach which gives a bit more control over the formatting:
    from lxml import etree
    etree.tostring(soap_message, pretty_print=True, xml_declaration=True, encoding='utf-8').decode()

(I learned the second approach by looking at the implementation of helper function etree_to_string.)

Hope this helps.

@prasanth-varikallu If you use the solution outlined by @andreixk the xml-envelope is saved in a text file named "test.xml" in the folder of your python program.
The print() funtion only returns the element location which in itself is good as it means that your code is creating an element.
Hope this helps.

@andreixk I need help how to save output of zip client service into xml right now its in json formate.
below is the command I am using, also attached snapshot of output I am getting
client.service.Ib440ConfigGet()
Snip20200921_111

Was this page helpful?
0 / 5 - 0 ratings