mvantellingen / python-zeep

A Python SOAP client
http://docs.python-zeep.org
Other
1.89k stars 587 forks source link

Retrieve etree.Element from zeep.xsd type #547

Open ArthurArslanaliev opened 7 years ago

ArthurArslanaliev commented 7 years ago

Since zeep uses lxml to parse XML, is there a way to retrieve etree.Element from zeep object? For instance, I have the following code:

from zeep import Client
from lxml import etree

client = Client(wsdl='https://webservices.netsuite.com/wsdl/v2013_1_0/netsuite.wsdl')

qname = '{urn:sales_2013_1.transactions.webservices.netsuite.com}SalesOrder'
sales_order_type = client.get_type(qname)

# Is there a way to reference etree.Element associated with this complexType? Something like:
# assert etree.iselement(sales_order_type._element) 

Thanks in advance!

lafrech commented 6 years ago

I'm interested in this as well.

I would like to dump an object into XML. I didn't find a way to do that with zeep. I suppose I could use tostring if I could get a hand on the corresponding etree.Element.

I know I can set raw_response=True to get a response as XML, but I want a single XML out of multiple requests, so I'm creating a parent object in zeep that contains a list of those element objects. I just don't know how to dump it into XML.

roy-j commented 4 years ago

wondering if the issue has somehow been resolved.

fabianoengler commented 4 years ago

Any updates on this?

I'm interested on this as well. Some services I am accessing with zeep have complex and deeply nested responses. Being able to query the responses xpath would make things a lot easier.

Zeep doesn't support xpath but since zeep uses lxml and and lxml.etree elements does support xpath (as does stdlib xml.etree) It should be possible to do so.

If an easy way is provided to access the underling lxml objects, a great many use cases would be readily available for you lib fellow consumers. And it would be much more efficient to access etree elements without extracting the raw xml (with raw_response=True) and re-parsing the whole response again.

lxml also has the objectify submodule, and objectified objects look awfully similar to zeep xsd objects, but in some context I find it useful to have access to the distinction of children and attributes that objectify provides.

Being able to have access to objectified responses would be even better I guess then pure etree objects, since it objectified elements seems to be a compatible superset of of etree Elements. (But if we can have access to etree objects it would also be trivial to get objectified instances from them).