How to convert suds object to xml string
<p>This is a duplicate to this question:
<a href="http://stackoverflow.com/questions/33303813/how-to-convert-suds-object-to-xml">How to convert suds object to xml</a>
<br>But the question has not been answered: "totxt" is not an attribute on the Client class. 
Unfortunately I lack of reputation to add comments. <br>So I ask again:
Is there a way to convert a suds object to its xml?</p>

<p>I ask this because I already have a system that consumes wsdl files and sends data to a webservice. But now the customers want to alternatively store the XML as files (to import them later manually). So all I need are 2 methods for writing data: One writes to a webservice (implemented and tested), the other (not implemented yet) writes to files.
If only I could make something like this:<br>
<code>xml_as_string = My_suds_object.to_xml()</code></p>

<p>The following code is just an example and does not run. And it's not elegant. Doesn't matter. I hope you get the idea what I want to achieve:
I have the function "write_customer_obj_webservice" that works. Now I want to write the function "write_customer_obj_xml_file".<br></p>

<pre><code>import suds

def get_customer_obj():
    wsdl_url = r'file:C:/somepathhere/Customer.wsdl'
    service_url = r'http://someiphere/Customer'
    c = suds.client.Client(wsdl_url, location=service_url)
    customer = c.factory.create("ns0:CustomerType")
    return customer

def write_customer_obj_webservice(customer):
    wsdl_url = r'file:C:/somepathhere/Customer.wsdl'
    service_url = r'http://someiphere/Customer'
    c = suds.client.Client(wsdl_url, location=service_url)
    response = c.service.save(someparameters, None, None, customer)
    return response

def write_customer_obj_xml_file(customer):
    output_filename = r'C\temp\testxml'
    # The following line is the problem. "to_xml" does not exist and I can't find a way to do it.
    xml = customer.to_xml()
    fo = open(output_filename, 'a')
    try:
        fo.write(xml)
    except:
        raise
    else:
        response = 'All ok'
    finally:
        fo.close()
    return response

# Get the customer object always from the wsdl.
customer = get_customer_obj()
# Since customer is an object, setting it's attributes is very easy. There are very complex objects in this system.
customer.name = "Doe J."
customer.age = 42
# Write the new customer to a webservice or store it in a file for later proccessing
if later_processing:
    response = write_customer_obj_xml_file(customer)
else:
    response = write_customer_obj_webservice(customer)
</code></pre>

<p>You have some issues in <code>write_customer_obj_xml_file</code> function:</p>

<p>Fix bad path:</p>

<pre><code>output_filename = r'C:\temp\test.xml'
</code></pre>

<blockquote>
  <p>The following line is the problem. "to_xml" does not exist and I can't find a way to do it.</p>
</blockquote>

<p>What's the type of customer? <code>type(customer)</code>?</p>

<pre><code>xml = customer.to_xml()  # to be continued...
</code></pre>

<p>Why mode='a'? ('a' => append, 'w' => create + write)</p>

<p>Use a <code>with</code> statement (file context manager).</p>

<pre><code>with open(output_filename, 'w') as fo:
    fo.write(xml)
</code></pre>

<p>Don't need to return a response string: use an exception manager. The exception to catch can be <a href="https://docs.python.org/2/library/exceptions.html#exceptions.EnvironmentError" rel="nofollow">EnvironmentError</a>.</p>

<h2>Analyse</h2>

<p>The following call:</p>

<pre><code>customer = c.factory.create("ns0:CustomerType")
</code></pre>

<p>Construct a <code>CustomerType</code> on the fly, and return a <code>CustomerType</code> instance <code>customer</code>.</p>

<p>I think you can introspect your <code>customer</code> object, try the following:</p>

<pre><code>vars(customer)  # display the object attributes
help(customer)  # display an extensive help about your instance
</code></pre>

<p>Another way is to try the WSDL URLs by hands, and see the XML results.
You may obtain the full description of your <code>CustomerType</code> object.</p>

<h2>And then?</h2>

<p>Then, with the attributes list, you can create your own XML. Use an XML template and fill it with the object attributes.</p>

<p>You may also found the magic function (<code>to_xml</code>) which do the job for you. But, not sure the XML format matches your need.</p>

python xml soap suds