I am using python2.6 and lxml, I want to add top level comments in xml like this
<?xml version='1.0' encoding='UTF-8'?>
<DCSubtitle/>
I use this addprevious () method for this, here is my code:
root = ET.Element("DCSubtitle")
root.addprevious(ET.Comment('top level comment'))
tree = ET.ElementTree(root)
tree.write(sys.stdout, pretty_print=True, xml_declaration=True, encoding='UTF-8')
But addprevious () seems not very logical, you need to add a second line and then add the first line, is there a better logical way to do this? Thank.
source
share