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
Thumbs Up Select Python comment (#) [solved] SpongeB0B 7 3,412 Sep-28-2025, 06:39 PM
Last Post: tester_V
  How to add multi-line comment section? Winfried 2 1,972 Jun-04-2024, 07:24 AM
Last Post: Gribouillis
Question remove all comment ? SpongeB0B 7 16,321 Oct-27-2023, 05:40 PM
Last Post: deanhystad
  Regular Expression search to comment lines of code Gman2233 5 3,772 Sep-08-2022, 06:57 AM
Last Post: ndc85430
  [SOLVED] [ElementTree] Grab text in attributes? Winfried 3 3,211 May-27-2022, 04:59 PM
Last Post: Winfried
  [ElementTree] Insert big block of HTML? Winfried 0 2,036 May-12-2022, 07:08 AM
Last Post: Winfried
  ElementTree get attribute value part of string paulo79 1 4,686 Apr-05-2022, 09:13 PM
Last Post: deanhystad
  Block comment in Intellij kam_uk 2 4,184 Nov-15-2020, 05:22 PM
Last Post: kam_uk
  xml.etree.ElementTree question. water 0 4,710 Oct-09-2020, 06:47 PM
Last Post: water
  xml.etree.ElementTree extract string values matthias100 2 9,658 Jul-12-2020, 06:02 PM
Last Post: snippsat

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020
This forum uses Lukasz Tkacz MyBB addons.
Forum use Krzysztof "Supryk" Supryczynski addons.