Joshkunz Joshkunz - 5 months ago 33
Python Question

Best way to generate xml?

I'm creating an web api and need a good way to very quickly generate some well formatted xml. I cannot find any good way of doing this in python.

Note: Some libraries look promising but either lack documentation or only output to files.

ars ars

Using lxml:

from lxml import etree

# create XML 
root = etree.Element('root')
# another child with text
child = etree.Element('child')
child.text = 'some text'

# pretty string
s = etree.tostring(root, pretty_print=True)
print s


  <child>some text</child>

See the tutorial for more information.