Write xml file using lxml library in Python

PythonXmlLxml

Python Problem Overview


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?

Python Solutions


Solution 1 - Python

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)

Solution 2 - Python

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")

Solution 3 - Python

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.

Solution 4 - Python

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)

Solution 5 - Python

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

etree.ElementTree(root).write('output.xml')

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionsystempuntooutView Question on Stackoverflow
Solution 1 - PythonmmmmmmView Answer on Stackoverflow
Solution 2 - PythonKairat KoibagarovView Answer on Stackoverflow
Solution 3 - PythonHarry MorenoView Answer on Stackoverflow
Solution 4 - PythonSoraya AnvariView Answer on Stackoverflow
Solution 5 - PythonkorakotView Answer on Stackoverflow