Python Forum

Full Version: Python 3 Elementtree and Comment First
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I am building an xml file using ElemetTree but I need to insert the encoding comment first.
Quote:<!--version="1.0" encoding="UTF-8"-->
It seems like I have to create the root node first and then add the comment after. I need to figure out how to add the comment before the root node. Can someone tell me how to put the comment as the first line in the xml file using ElementTree? TIA.
ET.Comment(text='<!--version="1.0" encoding="UTF-8"-->')
root=ET.Element('root')
With this code the comment does not show up. If I add the comment using 'root.insert' after setting the root node, it show up as the 2nd line.
Please show more code, everything up to and including the statements in your post would be good.
Do you actually want to insert a comment, or do you really want to insert the XML declaration?
You are correct. XML declaration. I didn't know about that method. That fixed it. Thanks.