Michael Sivak Michael Sivak - 1 year ago 385
Python Question

How can I return an XML file with Spyne?

I have Flask-Spyne server (web service) and I want to return (return to client after he will ask) a XML file.

I want to do this:

  • I will put some things from database to XML. (solved)

  • I have that XML saved on server (webservice) and I want to send it to client when he ask. (this is a problem)

Is there a way how to do it?

Here is mine code:

from flask import Flask
from flask_spyne import Spyne
from spyne.protocol.soap import Soap11
from spyne.model.primitive import Unicode, Integer, Float, DateTime
from spyne.model.complex import Iterable

app = Flask(__name__)
spyne = Spyne(app)

class dotazNaOracleDB(spyne.Service):
__service_url_path__ = '/soap/oracleservice';
__in_protocol__ = Soap11(validator='lxml');
__out_protocol__ = Soap11();

@spyne.srpc(DateTime, DateTime, _returns="What to put here?")
#from OracleDB_Procedure import UlozeniDoXML
s = open("Databaze.xml");
return s;

if __name__ == '__main__':
app.run(host = '');

Thank You very much for any help.. :)

Answer Source

If you want to return it as a regular string inside a document, you must set your _return type to Unicode.

If you want to return it as an xml document, you must parse it (etree.parse("Databaze.xml")) and return the resulting ElementTree instance. Your return type in this case should be AnyXml.

Also see these examples:



Sending as string isolates your document from parent context. It's a bit more inefficient (e.g. < character becomes &lt;) but otherwise harmless.

Sending as document makes your document part of the SOAP message. It's more efficient but makes it inherit namespace prefixes from the parent document which may cause slight changes to the document -- i.e. what you put in and what you get back out may not be equal byte-for-byte (but equivalent nevertheless).

It totally depends on the use case. If in doubt, return as string.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download