How do I create an xml document in python
<p>Here is my sample code:</p>

<pre><code>from xml.dom.minidom import *
def make_xml():
    doc = Document()
    node = doc.createElement('foo')
    node.innerText = 'bar'
    doc.appendChild(node)
    return doc
if __name__ == '__main__':
    make_xml().writexml(sys.stdout)
</code></pre>

<p>when I run the above code I get this:</p>

<pre><code>&lt;?xml version="1.0" ?&gt;
&lt;foo/&gt;
</code></pre>

<p>I would like to get:</p>

<pre><code>&lt;?xml version="1.0" ?&gt;
&lt;foo&gt;bar&lt;/foo&gt;
</code></pre>

<p>I just guessed that there was an innerText property, it gives no compiler error, but does not seem to work... how do I go about creating a text node?</p>

<p>Setting an attribute on an object won't give a compile-time or a run-time error, it will just do nothing useful if the object doesn't access it (i.e. "<code>node.noSuchAttr = 'bar'</code>" would also not give an error).</p>

<p>Unless you need a specific feature of <code>minidom</code>, I would look at <code>ElementTree</code>:</p>

<pre><code>import sys
from xml.etree.cElementTree import Element, ElementTree

def make_xml():
    node = Element('foo')
    node.text = 'bar'
    doc = ElementTree(node)
    return doc

if __name__ == '__main__':
    make_xml().write(sys.stdout)
</code></pre>
