A
A
Andrey Kovalchuk2017-08-25 16:25:31
Python
Andrey Kovalchuk, 2017-08-25 16:25:31

How to create xml in memory, attach it to json and send?

Good day.
Can you please tell me how to create xml in buffer, attach it to json and send?

def send_json(**kwargs):
    send_to = 'http://bla-bla.ru/external/'
    msg = {}

    msg.update(kwargs)
    context = MyObject.objects.get(pk=kwargs.get('id'))
    xml = render_to_string('xml_template_min.xml',
                           {'object': context})

    f = io.TextIOBase(xml, encoding='text/xml;name="myobject.xml"')
    r = requests.post(send_to, files=f, data=msg)

I imagined it something like this, but, apparently, I have the wrong idea.
render_to_string is a django function that returns a string.
Tell me how to do it right? Important: do not save the file in the system, everything is in memory.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Cheremisin, 2017-08-25
@leahch

>>> from xml.etree import ElementTree as ET
>>> root = ET.Element("root")
>>> ET.SubElement(root, 'b')
>>> ET.SubElement(root, 'a')
>>> ET.SubElement(root, 'c')
>>> ET.tostring(root)
'<root><b /><a /><c /></root>'

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question