Joshkunz Joshkunz - 1 year ago 92
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
Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download