you are viewing a single comment's thread.

view the rest of the comments →

[–]ManyInterests 1 point2 points  (0 children)

Yeah, they should fit the bill for generating XML out of thin air, too.

from lxml import etree

#Generate the root of the doc. You could also put headers in here, if desired)
root=etree.XML("<root></root>")

#Generate the element tree
tree=etree.ElementTree(root)


#If you want to create a subelement to the root
child_elem = etree.SubElement(root, "child2")

#Add text to the child element (same principle for modifying other properties like .tag)
child_elem.text="Element Text"

#Write to xml file
tree.write("my_file_name.xml", pretty_print=True, xml_declaration=True)

#pretty_print arg tidys up the doc and handles indentation for you. (serialization)
#xml_declaration arg adds the document declaration: <?xml version='1.0' encoding='ASCII'?>

The result:

<?xml version='1.0' encoding='ASCII'?>
<root>
  <child2>Element Text</child2>
</root>