0

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:

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

    @spyne.srpc(DateTime, DateTime, _returns="What to put here?")
    def Function(A,B):
        GetXML(A,B)
        s = open("file.xml");
        return s;

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

Thank You very much for any help.. :)

EDIT:

So, this is mine code now: (sending string)

@spyne.srpc(DateTime, DateTime, _returns=Iterable(Unicode))
    def oracle(A,B):
        GetXML(A,B)
        s = open("file.xml");
        return s

1 Answers1

1

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:

https://github.com/plq/spyne/blob/1f214d102913848cf9d18985d2d75ae1a97375de/examples/response_as_file_dynamic.py

https://github.com/plq/spyne/blob/1f214d102913848cf9d18985d2d75ae1a97375de/examples/response_as_xml_file.py

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.

Burak Arslan
  • 7,671
  • 2
  • 15
  • 24
  • Thank You very much :). For now, I did it with string. Can you tell me, if there is any difference in sending string or xml document? In speed, usability.. :) – Michael Sivak Jun 19 '16 at 14:02
  • So, If I am returning string and it is good for me now and I am satisfied, it is good and I dont have to change it :) Thank You very much – Michael Sivak Jun 19 '16 at 14:34
  • Sorry for interrupting again :) But could you write me, how it will look, if I want send xml document with AnyXml? I edited my post with string sending code.. Thank you – Michael Sivak Jun 19 '16 at 17:33
  • I already answered your question. If you want another answer, you need to ask another question. Or just read the docs, you know. – Burak Arslan Jun 21 '16 at 12:55
  • Ok, will do.. Thank you – Michael Sivak Jun 21 '16 at 13:12