tags:

views:

392

answers:

1

I'm using lxml to create an XML file from scratch; having a code like this:

from lxml import etree

root = etree.Element("root")
root.set("interesting", "somewhat")
child1 = etree.SubElement(root, "test")

How do i write root Element object to an xml file using write() method of ElementTree class?

+2  A: 

You can get a string from the element and then write that from lxml tutorial

str = etree.tostring(root, pretty_print=True)

or convert to an element tree

et = etree.ElementTree(root)
et.write(sys.stdout)
Mark
Thanks Mark, i was searching for the second solution.
systempuntoout