Python Forum
Python 3 Elementtree and Comment First
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python 3 Elementtree and Comment First
#1
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.
Reply
#2
Please show more code, everything up to and including the statements in your post would be good.
Reply
#3
Do you actually want to insert a comment, or do you really want to insert the XML declaration?
Reply
#4
You are correct. XML declaration. I didn't know about that method. That fixed it. Thanks.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to add multi-line comment section? Winfried 1 196 Mar-24-2024, 04:34 PM
Last Post: deanhystad
Question remove all comment ? SpongeB0B 7 1,321 Oct-27-2023, 05:40 PM
Last Post: deanhystad
  Regular Expression search to comment lines of code Gman2233 5 1,661 Sep-08-2022, 06:57 AM
Last Post: ndc85430
  [SOLVED] [ElementTree] Grab text in attributes? Winfried 3 1,629 May-27-2022, 04:59 PM
Last Post: Winfried
  [ElementTree] Insert big block of HTML? Winfried 0 1,179 May-12-2022, 07:08 AM
Last Post: Winfried
  ElementTree get attribute value part of string paulo79 1 2,136 Apr-05-2022, 09:13 PM
Last Post: deanhystad
  Block comment in Intellij kam_uk 2 2,291 Nov-15-2020, 05:22 PM
Last Post: kam_uk
  xml.etree.ElementTree question. water 0 3,278 Oct-09-2020, 06:47 PM
Last Post: water
  xml.etree.ElementTree extract string values matthias100 2 4,963 Jul-12-2020, 06:02 PM
Last Post: snippsat
  Youtube Comment bot mateusz135 4 66,307 Mar-14-2020, 04:18 PM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020