69

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?

systempuntoout
  • 71,966
  • 47
  • 171
  • 241

5 Answers5

89

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

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

Look at the tostring documentation to set the encoding - this was written in Python 2, Python 3 gives a binary string back which can be written directly to file but is probably not what you want in code.

or convert to an element tree (originally write to a file handle but either missed when I wrote this or it is new it can be a file name as per this answer )

et = etree.ElementTree(root)
et.write('output.xml', pretty_print=True)
mmmmmm
  • 32,227
  • 27
  • 88
  • 117
  • 3
    And `write` also accepts argument `pretty_print=True` http://lxml.de/tutorial.html – juanmirocks Mar 20 '13 at 17:54
  • 8
    when you write `str`, make sure you write in binary mode `b`: `with open('pretty.html', 'wb') as file: file.write(str)` – laviex Apr 04 '17 at 21:44
  • 3
    As of python3, you need to use `sys.stdout.buffer` instead of `sys.stdout` - which essentially is the same as what @laviex pointed out, only for the special case of `sys.stdout`. – kdb Sep 29 '17 at 09:02
  • 1
    I don't think this answers the question. It doesn't write to a file as requested. – Alex Spurling Jan 04 '18 at 16:09
  • @juanmirocks the tutorial says so, but in practice there's an error (`v4.2.0`) that `write got an unexpected keyword argument: pretty_print` – Ciprian Tomoiagă Oct 25 '18 at 17:52
  • I think et.write doesn't exist anymore – mokko Mar 22 '19 at 06:59
  • 1
    you can also get a real python `str` by using `etree.tostring(root, pretty_print=True, encoding="unicode")`. See [lxml.etree.tostring API doc](https://lxml.de/apidoc/lxml.etree.html#lxml.etree.tostring) – Cyril Waechter Oct 23 '20 at 13:55
  • @mokko this is lxml which has added to ElementTree it is in https://lxml.de/apidoc/lxml.etree.html#lxml.etree.tostring – mmmmmm Oct 23 '20 at 16:40
  • Can someone please explain the thinking behind whether to write binary or not? There are 4 variations in the answers: write using 'w' or 'wb' and writing the tree directly or using tree.tostring(). – John Nov 28 '20 at 23:11
  • @John that is a seperate question. However in this case note my edit re Python 2 and 3. One of the main differences between them is are you dealing with bytes or text whic needs an encoding. I think lxml write does it all for you. Try the code and ignore the comments for the moment. Then comment if it does not work – mmmmmm Nov 29 '20 at 10:48
  • Thanks @mmmmmm -- I had forgotten about Python 2. – John Nov 30 '20 at 02:55
13

You can try the below code.

from lxml import etree as ET

root = ET.Element('Doc')
level1 = ET.SubElement(root, 'S')
main = ET.SubElement(level1, 'Text')
main.text = 'Thanks for contributing an answer to Stack Overflow!'
second = ET.SubElement(level1, 'Tokens')
level2 = ET.SubElement(second, 'Token', word=u"low")


level3 = ET.SubElement(level2, 'Morph')
second1 = ET.SubElement(level3, 'Lemma')
second1.text = 'sdfs'
second1 = ET.SubElement(level3, 'info')
second1.text = 'qw'

level4 = ET.SubElement(level3, 'Aff')
second1 = ET.SubElement(level4, 'Type')
second1.text = 'sdfs'
second1 = ET.SubElement(level4, 'Suf')
second1.text = 'qw'

tree = ET.ElementTree(root)
tree.write('output.xml', pretty_print=True, xml_declaration=True,   encoding="utf-8")
Kairat Koibagarov
  • 1,385
  • 15
  • 9
8

Here's a succinct answer

from lxml import etree

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

my_tree = etree.ElementTree(root)
with open('./filename', 'wb') as f:
    f.write(etree.tostring(my_tree))

you simply place your node in a new tree and write that out to disk. Also works for HtmlElements produced by xpath searches.

Harry Moreno
  • 10,231
  • 7
  • 64
  • 116
6

This works for me:

et = etree.ElementTree(document)
with open('sample.xml', 'wb') as f:
    et.write(f, encoding="utf-8", xml_declaration=True, pretty_print=True)
Soraya Anvari
  • 179
  • 2
  • 5
3

You can give the filename to the write() of ElementTree

etree.ElementTree(root).write('output.xml')
korakot
  • 37,818
  • 16
  • 123
  • 144